项目中需要嵌入H5时(如商品详情),需要展示完整的webView时,提供两种获取的方式,这两种方式都需要配合UIWebView的代理方法使用.(建议使用第二种)
第一种:通过self.listHeaderWebView.scrollView.contentSize(偶获取不准)
<pre>-(void)webViewDidFinishLoad:(UIWebView *)webView{
if (webView.isLoading) {
return;
}
//获取加载完的高度
CGFloat htmlHeight = self.listHeaderWebView.scrollView.contentSize.height
NSLog(@"webViewDidFinishLoad---htmlHeight:%@",htmlHeight);
CGRect newFrame = webView.frame;
newFrame.size.height = htmlHeight;
self.webView.frame = newFrame;
}</pre>
第二种:通过JS获取
<pre>-(void)webViewDidFinishLoad:(UIWebView *)webView{
if (webView.isLoading) {
return;
}
//获取加载完的高度
NSString *htmlHeight = [self.listHeaderWebView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight"];
NSLog(@"webViewDidFinishLoad---htmlHeight:%@",htmlHeight);
CGRect newFrame = webView.frame;
newFrame.size.height = htmlHeight.floatValue;
self.webView.frame = newFrame;
}</pre>
网友评论