获得WebView的高度 iOS

 

-(void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation
{
    [webView evaluateJavaScript:@"document.body.scrollWidth"completionHandler:^(id _Nullable result,NSError * _Nullable error){
        CGFloat ratio =  CGRectGetWidth(webView.frame) /[result floatValue];
        //如果scrollHeight方法有问题 可以找webview商议获得lastLinediv试试
//        NSString *meta = @"document.getElementById(\"lastLinediv\").offsetTop";
        NSString *meta = @"document.body.scrollHeight";
        [webView evaluateJavaScript:meta completionHandler:^(id _Nullable result,NSError * _Nullable error){
            //webview的高度
            CGFloat newHeight = [result floatValue]*ratio;
            
        }];
    }];
}

 

posted @ 2021-09-07 11:33  黄增松  阅读(103)  评论(0编辑  收藏  举报