https://www.jianshu.com/p/aee2cfabaa79
> 方法一
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
CGFloat height = 0.0;
[webView sizeToFit];
height = webView.scrollView.ContentSize.height;
CGRect webFrame = webView.frame;
webFrame.size.height = height;
webView.frame = webFrame;
}
> 方法二
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
[webView evaluateJavaScript:@"document.body.offsetHeight" completionHandler:^(id data, NSError * _Nullable error) {
CGFloat height = [data floatValue];
//ps:js可以是上面所写,也可以是document.body.scrollHeight;在WKWebView中前者offsetHeight获取自己加载的html片段,高度获取是相对准确的,但是若是加载的是原网站内容,用这个获取,会不准确,改用后者之后就可以正常显示,这个情况是我尝试了很多次方法才正常显示的
CGRect webFrame = webView.frame;
webFrame.size.height = height;
webView.frame = webFrame;
}];
}
> 方法三:遍历WKWebView的所有子视图,找到中间的WKContentView,获取到它的frame设定给webView
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{
if(webView.subViews.count > 0) {
for(UIView *view in webView.subViews) {
if([view iskindOfClass:NSClassFromString:@"WKContentView"]){
webView.frame = view.frame;
}
}
作者:grand_max
链接:https://www.jianshu.com/p/aee2cfabaa79
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。