how to get correct hight when we have some content on UIWebView in swift iOS?

给你一囗甜甜゛ 提交于 2019-12-12 03:42:33

问题


I have disqusView(link) in UIWebView, I want to scroll webView as the size of disqusView, I don't want default scroll of webView

here is my code, in these I'm getting more height then actual height of webView content size.

func webViewDidFinishLoad(webView: UIWebView) {

 let cgfloat = web_discus.scrollView.contentSize.height as CGFloat
}

or

web_discus.stringByEvaluatingJavaScriptFromString("document.hight")! as String

but both give more height then actual that`s why it show more blank space while scrolling down..

I also tried this one :

web_discus.stringByEvaluatingJavaScriptFromString("document.body.offsetHeight")! as String

Is there any way to get perfect hight for webView content ?


回答1:


Seems you want the contentSize of the webView? If that is the case, then this other StackOverflow question should point you in the right direction link

Essentially you'd do webView.sizeThatFits(CGSizeZero).




回答2:


If I'm not mistaking, I think you should use window.pageYOffset:

if let myPostion = web_discus.stringByEvaluatingJavaScript(from: "window.pageYOffset"), let myPostionCGFloat = NumberFormatter().number(from: myPostion) {
        web_discus.scrollView.contentSize.height = CGFloat(myPostionCGFloat)
    }
}


来源:https://stackoverflow.com/questions/39993770/how-to-get-correct-hight-when-we-have-some-content-on-uiwebview-in-swift-ios

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