方法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