在我的应用程序中,我需要获取WKWebView中网页的高度。所以我使用下面的代码。
webView.evaluateJavaScript(("document.height"), completionHandler: { contentHeight,error in
print(contentHeight)
})在iOS 9之前,它会正确地返回网页高度。但在iOS 10中,contentHeight始终为nil。
我设置好了
webView.scrollView.scrollEnabled = falseweb视图在UITableViewCell中。
webView和UITableViewCell的高度是可变的。在web视图上有一个顶部和底部约束,以适应单元格。
当我得到网页高度时,我希望它反映在单元格的高度上。
发布于 2016-09-18 12:53:36
试试这个:
let javascriptString = "" +
"var body = document.body;" +
"var html = document.documentElement;" +
"Math.max(" +
" body.scrollHeight," +
" body.offsetHeight," +
" html.clientHeight," +
" html.offsetHeight" +
");"
webView.evaluateJavaScript(javascriptString) { (result, error) in
if error == nil {
if let result = result, let height = JSON(result).int {
self.htmlContentHeight = CGFloat(height)
self.resetContentCell()
}
}
}PS。WKWebiew渲染问题:WKWebView not rendering correctly in iOS 10
发布于 2016-10-25 09:26:04
您可以尝试使用
webView.evaluateJavaScript(("document.body.offsetHeight"), completionHandler:^(id _Nullable result, NSError * _Nullable error) {
NSLog(@">>>%@",result);
})https://stackoverflow.com/questions/39553560
复制相似问题