计算webView内容高度

// 禁止webView 滚动
UIScrollView *tempView = (UIScrollView *)[_webHeaderView.subviews objectAtIndex:0];
        tempView.scrollEnabled = NO;

计算 UIWebView的高度
- (UIWebView *)webHeaderView {
    if (!_webHeaderView) {
        _webHeaderView = [[UIWebView alloc] init];
        _webHeaderView.delegate = self;
        UIScrollView *tempView = (UIScrollView *)[_webHeaderView.subviews objectAtIndex:0];
        tempView.scrollEnabled = NO;
    }
    return _webHeaderView;
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    [HUDManager hideHUD];

    webView.scrollView.contentSize = CGSizeZero;
}

// 添加监听
    [self.webHeaderView.scrollView addObserver:self forKeyPath:@"contentSize" options:NSKeyValueObservingOptionNew context:nil];


- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if ([keyPath isEqualToString:@"contentSize"]) {
       CGFloat webViewH = [[self.webHeaderView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight"] floatValue] + 15;
        NSLog(@"现在的高度===%f",webViewH);
        
        self.webHeaderViewHeight = webViewH;
        
        if (self.commentDataMuArray.count) {
            [self addDataSource];
        }
        
        [self.tableView reloadData];
    }
}


你可能感兴趣的:(计算webView内容高度)