iOS UIWebView 加载图片完成后在图片底部添加自定义按

2020-02-20  本文已影响0人  明似水

公司有这样的一个需求,用webview加载显示图片,但是需要在图片的底部添加一个自定义的按钮,点击按钮进行下一步的操作。

废话不多说,直接上代码:
属性

@property (nonatomic , strong)UIWebView * webView;

添加子视图

if (!self.webView) {
        self.webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-bNavAllHeight)];
        self.webView.backgroundColor = [UIColor clearColor];
        self.webView.delegate = self;
        self.webView.scrollView.delegate = self;
        self.webView.tag = 1314;
        [self.view addSubview:self.webView];
        self.webView.scalesPageToFit=YES;
        NSString *path1 = [[NSBundle mainBundle] pathForResource:@"bbs_homeZhu" ofType:@"png"];
        if ([HBHuTool isJudgeString:path1]) {
            [app showToastView:@"没找到文件"];
            return;
        }
        NSURL *url = [NSURL fileURLWithPath:path1];
        
        NSURLRequest *request = [NSURLRequest requestWithURL:url];
        
        [self.webView loadRequest:request];
        
        //重新监听
        [self addObserverForWebViewContentSize];
    }

监听与添加按钮

- (void)addObserverForWebViewContentSize{
    [self.webView.scrollView addObserver:self forKeyPath:@"contentSize" options:0 context:nil];
}
- (void)removeObserverForWebViewContentSize{
    [self.webView.scrollView removeObserver:self forKeyPath:@"contentSize"];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    //在这里边添加你的代码
    [self layoutCell];
}
//设置footerView的合理位置
- (void)layoutCell{
    //取消监听,因为这里会调整contentSize,避免无限递归
    [self removeObserverForWebViewContentSize];
//    UIView *viewss = [self.view viewWithTag:99999];
    if (!self.button) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setTitle:@"立即咨询" forState:UIControlStateNormal];
        [button setTitleColor:white_Color forState:UIControlStateNormal];
        [button addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
        [button setBackgroundImage:[UIImage createImageWithColor:UIColorFromHex(0xE6E90E)] forState:UIControlStateNormal];
        button.tag = 99999;
        button.layer.masksToBounds = YES;
        button.layer.cornerRadius = 4;
        button.layer.masksToBounds = YES;
        button.layer.cornerRadius = 4;
        button.layer.borderColor = UIColorFromHex(0xE6E90E).CGColor;
        button.layer.borderWidth = 1.0f;
        button.userInteractionEnabled = YES;
        self.button = button;
        [self.webView.scrollView addSubview:button];
    }
    CGSize contentSize = self.webView.scrollView.contentSize;
    CGRect buttonFrame = self.button.frame;
    buttonFrame = CGRectMake(20, contentSize.height-90-kBottomSafeHeight, SCREEN_WIDTH-40, 40);;
    self.button.frame = buttonFrame;
    
    self.webView.scrollView.contentSize = CGSizeMake(contentSize.width, contentSize.height);
    //重新监听
    [self addObserverForWebViewContentSize];
}

点击事件

-(void)clickButton:(UIButton *)button{
    //点击事件
}

如果对你有帮助,点个赞吧

END.

上一篇下一篇

猜你喜欢

热点阅读