how to get correct hight when we have some content

2019-09-06 10:10发布

问题:

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)
    }
}