根据webview 的内容动态改变高度

由于项目需要,需要动态加载网页高度,在网上找了很多方法都不是很理想,比如说:

-(void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation

{

//  document.body.offsetHeight  document.body.scrollHeight  document.getElementById(\"content\").offsetHeight;

[webView evaluateJavaScript:@"document.body.offsetHeight" completionHandler:^(id _Nullable result, NSError * _Nullable error) {

//获取页面高度,并重置webview的frame

CGFloat documentHeight = [result doubleValue];

CGRect frame = webView.frame;

frame.size.height = documentHeight;

webView.frame = frame;

height = frame.size.height;

mainScrollview.contentSize = CGSizeMake(0, height);

NSLog(@"webheight\\\\\\\\%f    %f  %f",frame.size.height ,_DescriHeaderView.frame.size.height,mainScrollview.contentSize.height);

}];

}

网络上大多说的都是以上的方法,可是我的并项目中并不好用,于是发现了下面的方法,一下子就解决了。

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{

NSLog(@"11111");

CGRect frame = webViewkk.frame;

frame.size.height = webViewkk.scrollView.contentSize.height;

webViewkk.frame = frame;

mainScrollview.contentSize = CGSizeMake(MainScreenWidth, MaxY(webViewkk));

}

分享给大家,希望对你有帮助。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容