美文网首页
iOS UIWebView 加载图片完成后在图片底部添加自定义按

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

作者: 明似水 | 来源:发表于2020-02-20 16:10 被阅读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.

    相关文章

      网友评论

          本文标题:iOS UIWebView 加载图片完成后在图片底部添加自定义按

          本文链接:https://www.haomeiwen.com/subject/atbxqhtx.html