I’ve carried out WKWebView inside UITableViewCell. Initially I used to be tried with UITableView.automaticDimension for getting wrapped content material top after loaded url into the net view. However appears getting content material top solely 44 pixel. So I’ve searched lot and tried with getting net view content material top through the use of evaluateJavaScript with “doc.readyState” additionally with “doc.getElementById(‘svelte’).scrollHeight” contained in the prepared state completion handler. This time used the “DivId = svelte” and acquired the precise contentHeight. As a result of If I attempt with “doc.physique.scrollHeight” and many others,.. I am getting top too large. In order that’s why I am utilizing “DivID” – I took reference from right here: https://gist.github.com/pkuecuekyan/f70096218a6b969e0249427a7d324f91?permalink_comment_id=3642554#gistcomment-3642554
I acquired actual top as I mentioned above on my command. However my drawback is like as soon as loaded the url into net view, in backside facet it having buttons like “+” image animating with wave. If click on on that button, It can show the popup with extra info. So initially after loaded the url, net view getting top like “2025” then after clicked on buttons(+ image) after displayed popup with extra info means, this time net view scroll content material measurement must be elevated with 400 to 500 pixels. I want these info when the net view resized with extra content material top. Kindly test the hooked up display pictures and my code too.[![After Initial loaded.[![Once clicked the plus symbol button. displayed popup with additional information. But UITableViewCell content height was not updated.[![This screen shot represent the popup with additional information. Becuase I have enabled scroll inside web view. But I’m expecting updated content height after done click action on button which is on back end side.
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
debugPrint("scroll view content size height on didFinish ==> (webView.scrollView.contentSize.height) & webView Height => (webView.bounds.height)")
///Regarding for zooming need to evaluate viewport java script.
webView.evaluateJavaScript("var meta = document.createElement('meta');meta.setAttribute('name', 'viewport');meta.setAttribute('content', 'width=device-width, shrink-to-fit=YES, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no');document.getElementsByTagName('head')[0].appendChild(meta);", completionHandler: nil)
///Relating to for getting dynamic top.
webView.evaluateJavaScript("doc.readyState", completionHandler: { (full, error) in
if full != nil {
///Getting top too large contained in the UITableViewCells, the instructed options did not work constantly. In my case querying particular div as an alternative of the entire physique appears to work nice. Because of this reference: https://gist.github.com/pkuecuekyan/f70096218a6b969e0249427a7d324f91?permalink_comment_id=3642554#gistcomment-3642554
webView.evaluateJavaScript("doc.getElementById('svelte').scrollHeight", completionHandler: { (top, error) in
if let heightUW = top as? CGFloat {
RichContentTableViewCell.maximumHeight = heightUW
}
debugPrint("Error on doc.getElementById div id -> svelte ==> (String(describing: error))")
})
}
debugPrint("Error on doc.readyState ==> (String(describing: error))")
})
}
](https://i.stack.imgur.com/PhF8D.jpg)](https://i.stack.imgur.com/PhF8D.jpg)](https://i.stack.imgur.com/W3pKj.jpg)](https://i.stack.imgur.com/W3pKj.jpg)](https://i.stack.imgur.com/kmuYx.jpg)](https://i.stack.imgur.com/kmuYx.jpg)
And likewise I’ve tried with “KVO” on WKWebView scrollView Content material Measurement. This time Im getting actual top solely If achieved scroll down and goto high on vertical scroll. Then solely UITableViewCell updating with newest content material top.
webView.scrollView.addObserver(self, forKeyPath: "contentSize", choices: .new, context: nil)
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if (object as? UIScrollView) == self.webView.scrollView, keyPath == "contentSize" {
debugPrint("New contentSize top : %f", self.webView.scrollView.contentSize.top);
if RichContentTableViewCell.maximumHeight != self.webView.scrollView.contentSize.top {
RichContentTableViewCell.maximumHeight = self.webView.scrollView.contentSize.top
}
}
}
deinit {
self.webView.scrollView.removeObserver(self, forKeyPath: "contentSize", context: nil)
}