无法获得 uiwebview 高度的正确值

Can't get the right value for height of uiwebview

我正在尝试通过这种方式获取 uiwebview 的值:

            var webview = self.articleContent

            println(webview.frame.size.height)
            // prints 300

            // rendering the webview
            webview.loadHTMLString(articleHtmlContent, baseURL: nil)

            // getting the height of the webview
            var output = webview.stringByEvaluatingJavaScriptFromString("document.body.scrollHeight;")!

            println(output)
            // prints 300 - again

所以看起来我得到的输出是 uiwebview 的高度而不是 HTML 内容的高度。

如何获取 HTML 内容的高度?

使用其滚动视图的内容大小,该内容大小将在 Web 视图在其 UIWebViewDelegate 上响应 webViewDidFinishLoad 后可用,如下所示:

func webViewDidFinishLoad(webView: UIWebView!) {
  CGFloat totalHeight = webview.scrollView.contentSize.height
}