美文网首页
wkwebView 通过scrollHeight获取高度不准确处

wkwebView 通过scrollHeight获取高度不准确处

作者: 辉546 | 来源:发表于2019-10-09 11:54 被阅读0次

    在webView中先设置下列HTML代码,让webView能完美适配富文本的内容,之后取到的高度就是准确的高度。

     NSString *str = [model.getModel stringByAppendingString: @"<html><head><meta content=\"width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0\" name=\"viewport\"><style type=\"text/css\">img{display: inline-block;max-width:100%}</style></head><body></body></html>"];
        [self.webView loadHTMLString:str baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle]bundlePath]]];
    

    获取wkwebView准确高度代码:

    //加载完成之后调用
    -(void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation{
    
        self.webView.scrollView.scrollEnabled = NO;
    //    [webView evaluateJavaScript:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '200%'" completionHandler:nil];
        [webView evaluateJavaScript:@"document.documentElement.scrollHeight"completionHandler:^(id _Nullable result,NSError * _Nullable error){
            SSLog(@"scrollHeight高度:%.2f",[result floatValue]);
            if (self.webHeight <= 0) {
                self.webHeight = [result floatValue];//获取的精准高度
                //刷新方法
            }
        }];
    }
    

    相关文章

      网友评论

          本文标题:wkwebView 通过scrollHeight获取高度不准确处

          本文链接:https://www.haomeiwen.com/subject/crrbuctx.html