/////////////////////////////初始化,self.view是父控件/////////////////////////////////_webView = [[UIWebView alloc] initWithFrame: CGRectMake(0,0, self.view.frame.size.width,0)];
_webView.delegate=self;
_webView.scrollView.bounces=NO;
_webView.scrollView.showsHorizontalScrollIndicator=NO;
_webView.scrollView.scrollEnabled=NO;[_webView sizeToFit];
///////////////////////////////设置内容,这里包装一层div,用来获取内容实际高度(像素),htmlcontent是html格式的字符串//////////////NSString * htmlcontent = [NSString stringWithFormat:@"
%@", htmlcontent];[_webView loadHTMLString:htmlcontent baseURL:nil];////////////////////////////////delegate的方法重载////////////////////////////////////////////- (void)webViewDidFinishLoad:(UIWebView *)webView
{//获取页面高度(像素)
NSString * clientheight_str = [webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight"];floatclientheight =[clientheight_str floatValue];
//设置到WebView上
webView.frame = CGRectMake(0,0, self.view.frame.size.width, clientheight);//获取WebView最佳尺寸(点)CGSize frame =[webView sizeThatFits:webView.frame.size];
//获取内容实际高度(像素)
NSString * height_str= [webView stringByEvaluatingJavaScriptFromString:@"document.getElementById('webview_content_wrapper').offsetHeight + parseInt(window.getComputedStyle(document.getElementsByTagName('body')[0]).getPropertyValue('margin-top')) + parseInt(window.getComputedStyle(document.getElementsByTagName('body')[0]).getPropertyValue('margin-bottom'))"];
floatheight =[height_str floatValue];
//内容实际高度(像素)* 点和像素的比
height = height * frame.height /clientheight;
//再次设置WebView高度(点)
webView.frame = CGRectMake(0,0, self.view.frame.size.width, height);
}
iOS webView自适应内容高度(完美版)
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- NSString * clientheight_str = [webView stringByEvaluating...
- UITableView的Group样式下顶部空白处理分组列表头部空白处理UIView*view = [[UIVie...