欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

iOS Webview自适应实际内容高度的4种方法详解

程序员文章站 2023-12-16 14:56:34
//第一种方法 - (void)webviewdidfinishload:(uiwebview *)webview { cgfloat webviewheig...

//第一种方法

- (void)webviewdidfinishload:(uiwebview *)webview
{
cgfloat webviewheight=[webview.scrollview contentsize].height;
cgrect newframe = webview.frame;
newframe.size.height = webviewheight;
webview.frame = newframe;
_webtablewview.contentsize = cgsizemake(320, newframe.size.height + 64 + kwidth - 100);
}

//2.执行js语句 直接获取html文档的dom高度

- (void)webviewdidfinishload:(uiwebview *)webview{
cgfloatwebviewheight =[[webviewstringbyevaluatingjavascriptfromstring:@document.body.offsetheight]floatvalue];
// cgfloat webviewheight= [[webviewstringbyevaluatingjavascriptfromstring:@document.body.scrollheight]floatvalue];
cgrectnewframe = webview.frame;
newframe.size.height= webviewheight;
webview.frame= newframe;
}

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

-(void)webviewdidfinishload:(uiwebview*)webvie{
cgsize actualsize = [webview sizethatfits:cgsizezero];
cgrect newframe = webview.frame;
newframe.size.height = actualsize.height;
webview.frame = newframe;
}

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

-(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 的高度
}
}
}
}

以上所述是小编给大家介绍的ios webview自适应实际内容高度的4种方法详解,希望对大家有所帮助

上一篇:

下一篇: