Sabish.M
Sabish.M

Reputation: 2060

WebView height not changed after update the constraint constant

I am using webview inside the uicollectionview cell. I update the webview height constraint constant after the html was loaded. in following way.

public func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        print("Load Completed")
        webView.evaluateJavaScript("document.readyState", completionHandler: { (complete, error) in
            if complete != nil {
                self.webview.evaluateJavaScript("document.body.offsetHeight", completionHandler: { (height, error) in

                    DispatchQueue.main.async {
                        self.webviewHeightConstraint.constant = height as! CGFloat
                        print("Height ",height as! CGFloat)
                        self.webview.setNeedsUpdateConstraints()
                    }
                })
            }
        })
    }

it give correct height value but ui is not changed until i scrolled little. But i start the scroll, webview height changed correctly. What is the issue here. Please help me.

Thank you,

Upvotes: 0

Views: 1096

Answers (2)

Milan Aghera
Milan Aghera

Reputation: 87

Migrating to WKWebView

The full code for WKWebView would then be:

func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
    webView.frame.size.height = 1
    webView.frame.size = webView.scrollView.contentSize
}

Upvotes: 0

Shehata Gamal
Shehata Gamal

Reputation: 100533

Replace

self.webview.setNeedsUpdateConstraints()

with

self.view.layoutIfNeeded()

Upvotes: 1

Related Questions