最近做类似新闻详情页,其中用到webview,需要计算出webview高度,将其添加到scrollview,以达到原生效果。
网上看到的例子大概如下:
方法1:在webviewdidfinishload里执行如下js代码;
-(void)webViewDidFinishLoad:(UIWebView *)webView{
float height = [[webViewstringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight;"]floatValue];
//document.body.scrollHeight
}
或者 NSString * height = [webView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight"];
方法二:
-(void) webViewDidFinishLoad:(UIWebView *)webView
{
CGRect frame =webView.frame;
CGSize fittingSize = [webViewsizeThatFits:CGSizeZero];
frame.size = fittingSize;
webView.frame = frame;
}