首页 技术 正文
技术 2022年11月15日
0 收藏 758 点赞 4,024 浏览 1565 个字

有的时候会碰见类似的苦逼需求, webview自适应实际内容高度 下面有四种方法供使用

方法1:获取webview中scrovllview的contentsize进行设置

 

123456 -(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高度

 

1234567 -(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;}

方法3.先将UIWebView的高度设为最小,然后再使用sizeThatFits就会返回刚好合适的大小

 

123456 -(void)webViewDidFinishLoad:(UIWebView *)webView{    CGSize actualSize = [webView sizeThatFits:CGSizeZero];    CGRect newFrame = webView.frame;    newFrame.size.height = actualSize.height;    webView.frame = newFrame;}

方法4.遍历webview子视图 获取UIWebDocumentView高度即实际高度

 

12345678910111213141516 -(void)webViewDidFinishLoad:(UIWebView *)webView{    CGFloat webViewHeight = 0.0f;    if ([webView.subviews count] > 0)    {        UIView *scrollerView = webView.subviews[0];        if ([scrollerView.subviews count] > 0)        {            UIView *webDocView = scrollerView.subviews.lastObject;            if ([webDocView isKindOfClass:[NSClassFromString(@”UIWebDocumentView”) class]])            {                webViewHeight = webDocView.frame.size.height;//获取文档的高度                webView.frame= webDocView.frame; //更新UIWebView 的高度            }        }    }}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,487
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,903
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,736
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,486
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,126
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,289