在webViewDidFinishLoad方法中实现
- (void)webViewDidFinishLoad:(UIWebView *)wb{
// 如果要获取webView内容高度必须在网页加载完成之后获取
self.webViewHeight = [[self.webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight"] floatValue];
//NSLog(@"------->:%f",self.webViewHeight);
self.webView.frame = CGRectMake(0, 0, Width,self.webViewHeight);
[(UIScrollView *)[[self.webView subviews] objectAtIndex:0] setBounces:NO];
[(UIScrollView *)[[self.webView subviews] objectAtIndex:0] setScrollEnabled:NO];
[self.tableView reloadData];
}
在webViewDidFinishLoad之后获取到网页内容高度的变化?
给webView的scrollView的contentSize属性添加监听,每当内容发生变化,contentSize一定会跟着变,捕获这个变动,在监听方法中实现webViewDidFinishLoad中的代码,也就是获取最新的内容高度赋给webView:
[self.webView.scrollView addObserver:self forKeyPath:@"contentSize" options:NSKeyValueObservingOptionNew context:nil];
//监听contentSize,获取web界面的size-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary*)change context:(void *)context
{
if ([keyPath isEqualToString:@"contentSize"]) {
CGSize webSize = [self.webView sizeThatFits:CGSizeZero];
//NSLog(@"----->:%f",webSize.height);
self.webViewHeight = webSize.height;
self.webView.frame = CGRectMake(0, 0, Width,self.webViewHeight);
[(UIScrollView *)[[self.webView subviews] objectAtIndex:0] setBounces:NO];
[(UIScrollView *)[[self.webView subviews] objectAtIndex:0] setScrollEnabled:NO];
[self.tableView reloadData];
}
}
网友评论