I\'ve been looking around and wasnt able to see any swift related ways to do this. I\'m trying to get my UIWebViews height to be dynamic. I have a UIWebView that loads data
I had problem with:
let height = webView.scrollView.contentSize.height
Sometimes my web view height simply didn't calculacte and stayed on default value. So finally I manage to find better solution that works fine, just replace that line code with:
let height = webView.stringByEvaluatingJavaScript(from: "document.body.scrollHeight")
This is mine final complete code:
func webViewDidFinishLoad(_ webView: UIWebView) {
//webview height
webView.frame.size.height = 1
webView.frame.size = webView.sizeThatFits(.zero)
webView.scrollView.isScrollEnabled = false
let height = webView.stringByEvaluatingJavaScript(from: "document.body.scrollHeight")
if let height = height {
if let heightInt = Int(height) {
let heightFloat = Float(heightInt)
webViewHeightConstraint.constant = CGFloat(heightFloat)
}
}
webView.scalesPageToFit = true
}