添加WebView的代理方法,在webViewDidFinishLoad方法里面调用 stringByEvaluatingJavaScriptFromString方法得到webView的高度。[使用 stringByEvaluatingJavaScriptFromString方法,需要等UIWebView中的页面加载完成之后去调用]
#pragma mark ---- 数据加载完调用webView代理方法 - (void)webViewDidFinishLoad:(UIWebView *)aWebView { [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; CGRect frame = aWebView.frame; //webView的宽度 frame.size = CGSizeMake(300, 0); aWebView.frame = frame; float content_height = [[aWebView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight;"] floatValue]; frame = aWebView.frame; //webView的宽度和高度 frame.size = CGSizeMake(300, content_height+40); aWebView.frame = frame; NSLog(@"-----%d",(int) frame.size.height); }