方法1:获取webview中scrovllview的contentsize进行设置
-(void)webViewDidFinishLoad:(UIWebView *)webView{
CGFloat webViewHeight=[webView.scrollView contentSize].height;
CGRect newFrame = webView.frame;
newFrame.size.height = webViewHeight;
webView.frame = newFrame;
}方法2:执行js语句 直接获取html文档的dom高度
-(void)webViewDidFinishLoad:(UIWebView *)webView{
CGFloat webViewHeight= [[webView stringByEvaluatingJavaScriptFromString: @"document.body.offsetHeight"]floatValue];
// CGFloat webViewHeight= [[webView stringByEvaluatingJavaScriptFromString: @"document.body.scrollHeight"]floatValue];
CGRect newFrame = webView.frame;
newFrame.size.height = webViewHeight;
webView.frame = newFrame;
}原文地址为:http://www.skyfox.org/ios-webview-autofit-content-height.html
本文介绍两种方法来使 iOS WebView 自动调整其高度以适应内部内容的高度。第一种方法是通过获取 ScrollView 的 contentsize 进行设置;第二种方法是执行 JS 语句直接获取 HTML 文档的 dom 高度。
865

被折叠的 条评论
为什么被折叠?



