计算WKWebview的实际高度

网上有很多计算webview的真实高度不是等于零就是很大,我也不知道为什么,希望有大神指点,我这里写了一个观察者计算webview的例子,大家可以下载试试。

[_iWebView.scrollView addObserver:self forKeyPath:@"contentSize" options:NSKeyValueObservingOptionNew context:nil];

- (void)observeValueForKeyPath:(NSString *)keyPath
                      ofObject:(id)object
                        change:(NSDictionary *)change
                       context:(void *)context
{
    if (object == self.iWebView.scrollView && [keyPath isEqual:@"contentSize"]) {
        // we are here because the contentSize of the WebView's scrollview changed.
        UIScrollView *scrollView = self.iWebView.scrollView;
        NSLog(@"New contentSize: %f x %f", scrollView.contentSize.width, scrollView.contentSize.height);
        CGFloat webHeight = scrollView.contentSize.height;
        }
   
    }
}

最后别忘记移除观察者

-(void)dealloc
{
  if(_iWebView)
  {
    [self.iWebView.scrollView removeObserver:self forKeyPath:@"contentSize" context:nil];
  }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容