iOS10 WKWebview-EvaluationJavaScript("document.height")返回nil [英] iOS10 WKWebview - evaluateJavaScript("document.height") return nil
问题描述
在我的应用中,我需要在 WKWebView
中获取网页的高度.所以我使用下面的代码.
In my app, I need to get the height of a webpage in a WKWebView
. So I use the code below.
webView.evaluateJavaScript(("document.height"), completionHandler: { contentHeight,error in
print(contentHeight)
})
在iOS 9之前,这将正确返回网页高度.但是在iOS 10中, contentHeight
始终为 nil
.
Until iOS 9, this returns the webpage height correctly. But in iOS 10, contentHeight
is always nil
.
我已经设置
webView.scrollView.scrollEnabled = false
,Web视图位于 UITableViewCell
中.
webView
和 UITableViewCell
的高度是可变的.网络视图上有一个顶部和底部约束,以适合该单元格.
The height of webView
and of the UITableViewCell
are variable. There's a top and bottom constraint on the web view to fit to the cell.
当我获得网页高度时,我希望它能反映在单元格的高度中.
When I get the webpage height, I want it to be reflected in the cell's height.
推荐答案
尝试一下:
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在iOS 10中无法正确呈现
PS. WKWebiew rendering problem: WKWebView not rendering correctly in iOS 10
这篇关于iOS10 WKWebview-EvaluationJavaScript("document.height")返回nil的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!