美文网首页
iOS WKWebView 添加头部、尾部视图

iOS WKWebView 添加头部、尾部视图

作者: Mark2Win帅帅陈贤敏 | 来源:发表于2020-07-04 12:14 被阅读0次


一、添加头部视图

self.headView =[ [UIView alloc]initWithFrame:CGRectMake(0,-headHeight,RHScreenW,headHeight)];

self.webView.scrollView.contentInset = UIEdgeInsetsMake(headHeight,0,0,0);

[self.webView.scrollVIew addSubView:self.headView];

二、添加尾部视图

先创建尾部视图

_footerView = [[UIView alloc]initWithFrame:CGRectMake(0,0, RHScreenW,bottomHeight)];

self.webView.scrollView.contentInset = UIEdgeInsetsMake(0,0,bottomHeight,0);

[self.webView.scrollView addSubview:_footerView];

需要用到observer

1.添加observer

[_webView addObserver:self forKeyPath:@"scrollView.contentSize" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:@"DJWebKitContext"];

2.移除observer

-(void)dealloc{

[self.webView removeObserver:self forKeyPath:@"scrollView.contentSize" context:@"DJWebKitContext"];

}

3.observer事件

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{

    if ([keyPath isEqualToString:@"scrollView.contentSize"]){

        CGFloat webViewContentHeight = self.myWebView.scrollView.contentSize.height;

//        NSLog(@"webViewContentHeight:%f",webViewContentHeight);

        CGRect frame = self.footerView.frame;

        frame.origin.y = webViewContentHeight;

        self.footerView.frame = frame;

    }

}

望此文对您有帮助,谢谢阅读。

相关文章

网友评论

      本文标题:iOS WKWebView 添加头部、尾部视图

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