iOS WKWebView 添加头部、尾部视图

2020-07-04  本文已影响0人  Mark2Win帅帅陈贤敏


一、添加头部视图

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;

    }

}

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

上一篇 下一篇

猜你喜欢

热点阅读