calculating UIWebView height with images gives wrong answer

岁酱吖の 提交于 2019-12-25 03:35:21

问题


I found how to calculate dynamic uiwebview height according to its content. My code is :

  println("webViweDidFinish started")

    var frame:CGRect = myWebView.frame

    frame.size.height = 1

    var fittingSize :CGSize = myWebView.sizeThatFits(CGSizeZero)

    frame.size = fittingSize

    myWebView.frame = frame


    var contentsizeHeight = webView.scrollView.contentSize.height

    var yCoordinateOfWebView = myWebView.frame.origin.y


    var contentHeight: CGFloat = webView.scrollView.contentSize.height;

    myScrollView.contentSize = CGSizeMake(self.view.frame.width, yCoordinateOfWebView + contentsizeHeight + labelAuthorOfArticle.frame.height) //

    UIView.animateWithDuration(0.5, animations: {
        self.heightConstraints.constant = fittingSize.height

        self.myWebView.layoutIfNeeded()

        self.myScrollContentView.layoutIfNeeded()

    })



    self.activityIndicator.stopAnimating()

If I use this approach:

        var height:NSString! = webView.stringByEvaluatingJavaScriptFromString("document.height;")

    var heightInFloat = height.floatValue

How to convert height to CGFloat? I can only convert it to float as you see.

The problem is that when webview has too many images the height is calculated wrong. Oppositely when there is no image height is calculated right. When I reload the function, the second time it calculates right (with images). Here is content of string that I am loading:

 class func formatContentText(inout text: NSString)-> NSString{

    text = text.stringByReplacingOccurrencesOfString("src=\"", withString: "src=\"http://dixinews.kz")
    var deviceWidth = "300"
    text = "<html><head><meta name = \"viewport\" content = \"user-scalable=no, width=" + deviceWidth + " , maximum-scale=1.0\"><style>img{max-width:100%%;height:auto !important;width:auto !important;};</style></head><body>" + text + "</body></html>"

    return text
}

回答1:


I used approach with javascript. Here is my code:

  func webViewDidFinishLoad(webView: UIWebView) {





    var frame:CGRect = myWebView.frame



    // javascript
    var height:NSString! = webView.stringByEvaluatingJavaScriptFromString("document.height;")

    var heightInFloat = height.floatValue // convert to float

    var heightINCGFloat = CGFloat(heightInFloat) convert to cgfloat

    frame.size.height = heightINCGFloat //set heigh

    myWebView.frame = frame // set frame

    myWebView.scrollView.contentSize.height = myWebView.frame.height

    var contentsizeHeight = webView.scrollView.contentSize.height

    var yCoordinateOfWebView = myWebView.frame.origin.y

    myScrollView.contentSize = CGSizeMake(self.view.frame.width, yCoordinateOfWebView + contentsizeHeight + labelAuthorOfArticle.frame.height) //

    UIView.animateWithDuration(0.5, animations: {
        self.heightConstraints.constant = heightINCGFloat

        self.myWebView.layoutIfNeeded()

        self.myScrollContentView.layoutIfNeeded()

    })

    self.activityIndicator.stopAnimating()


}


来源:https://stackoverflow.com/questions/30732959/calculating-uiwebview-height-with-images-gives-wrong-answer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!