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

公司有这样的一个需求,用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.

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • [鱼之乐]义兴鱼语 西充山中度日月,义兴水滨遗鱼心。缱绻于金岭,久而往往滋生一种颓废,品不出风的味儿;来往于校与家...
    糊涂炊烟阅读 3,547评论 0 0
  • 生活如蓝调般的苦难 如果你向往黑暗 我们就扑灭光明 我们排成一排 跪向生活 缴械投降 平庸开枪 杀死了一个又一个 ...
    智野斯基阅读 1,850评论 0 5
  • 乘着每周不多的休息的日子,把欢乐喜剧人的第二季的节目看完了。我喜欢看喜剧搞笑类节目,为什么喜欢看喜剧呢?一来多数喜...
    小钿锣鼓阅读 3,110评论 0 0
  • 睡不着觉看百度云的相册,这是什么神仙2017呀。都是太美好的回忆了。 一月,和向老板旅游团一起看电影,吃日料跨年。...
    相闻离歌蝉时雨阅读 3,499评论 0 3
  • 自卑感与优越感 1、所有精神有疾患的人都有自卑情结,他们都会在某种特定的情形下感到无法过一种有用的生活,会为自己的...
    爱就是我我就是爱阅读 2,924评论 0 1

友情链接更多精彩内容