由于项目需要,需要动态加载网页高度,在网上找了很多方法都不是很理想,比如说:
-(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));
}
分享给大家,希望对你有帮助。