对于wkwebview网页高度计算的补充/网页高度计算的另一种简单方法

http://blog.csdn.net/flg1554112450/article/details/77334069

在这篇文章中,,提到计算网页高度的方法是监听,


[self.newsWebView addObserver:self forKeyPath:@"scrollView.contentSize"options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOldcontext:@"DJWebKitContext"];


#pragma mark 监听网页的内容高度

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

    if (!self.newsWebView.isLoading) {

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

            self.newsWebView.frame = CGRectMake(00WIDTH,self.newsWebView.scrollView.contentSize.height);

        }

    }

}


当如果监听出现了问题,,比如在plus机型上不知什么原因 页面发生抖动,,,此时就要换一种方法。。这种方法更简单一些

#pragma mark 网页加载完成
- ( void )webView:( WKWebView *)webView didFinishNavigation:( WKNavigation *)navigation{
    [ self performSelector : @selector (loadHeight) withObject : self afterDelay : 1 ];
}
一定要在1s延时之后 才能正确获取网页高度 ,,,虽然有一秒的延时,,但不影响正常的时候,,可以尝试把时间缩短 也应该是可以的
- ( void )loadHeight{
    CGFloat height = 0.0 ;
    [ self . detailWebView sizeToFit ];
    height = self . detailWebView . scrollView . contentSize . height ;
    CGRect webFrame = self . detailWebView . frame ;
    webFrame. size . height = height;
    self . detailWebView . frame = webFrame;
}

猜你喜欢

转载自blog.csdn.net/flg1554112450/article/details/78356262
今日推荐