简体   繁体   English

无法获得uiwebview高度的正确值

[英]Can't get the right value for height of uiwebview

I am trying to get the value of uiwebview this way: 我试图通过这种方式获取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

So it looks like the output I get is the height of the uiwebview instead of the height of the HTML content. 所以看起来我得到的输出是uiwebview的高度而不是HTML内容的高度。

How can I get the height of the HTML contents? 如何获得HTML内容的高度?

Use its scroll view's content size which will be available after the web view responds to webViewDidFinishLoad on its UIWebViewDelegate like this: 使用其滚动视图的内容大小,该视图将在Web视图在其UIWebViewDelegate上响应webViewDidFinishLoad后可用,如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM