计算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];
  }
}

你可能感兴趣的:(计算WKWebview的实际高度)