Reputation: 3
I added UILabel and WKWebView in vertical UIStackview and the distribution is set to fillProportionally
. I am trying to set the web view height based on the content height. For that, I set the initial web view height as 0. Once the html content is loaded, I am trying to update the height constraint of web view to the scroll height. But I am getting auto layout constraint conflict issues.
//Adding WKWebView in UIStackView
let headerString = "<header><meta name='viewport' content='width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no'></header>"
let htmlString = headerString + correctAnswer
questionWebView.loadHTMLString(htmlString, baseURL: nil)
stackView.addArrangedSubview(questionWebView)
questionWebView.translatesAutoresizingMaskIntoConstraints = false;
questionWebView.trailingAnchor.constraint(equalTo: stackView.trailingAnchor).isActive = true
questionWebView.leadingAnchor.constraint(equalTo: stackView.trailingAnchor).isActive = true
questionWebVieHeightAnchor = questionWebView.heightAnchor.constraint(equalToConstant: 0.0)
questionWebVieHeightAnchor.isActive = true
I tried to update height constraint dynamically in webView didFinish method. But I am getting layout conflict issue.
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
webView.evaluateJavaScript("document.readyState", completionHandler: { (complete, error) in
if complete != nil {
webView.evaluateJavaScript("document.body.scrollHeight", completionHandler: { (height, error) in
print("Height is \(height as! CGFloat)")
self.questionWebVieHeightAnchor.constant = height
self.questionWebView.layoutIfNeeded()
})
}
})
}
Issue:
Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want.
Try this:
(1) look at each constraint and try to figure out which you don't expect;
(2) find the code that added the unwanted constraint or constraints and fix it.
(
"<NSLayoutConstraint:0x600000a194a0 WKWebView:0x7f9363b5fa00.height == 200>",
"<NSLayoutConstraint:0x600000ad2b70 WKWebView:0x7f9363b5fa00.height == 0>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x600000a194a0 WKWebView:0x7f9363b5fa00.height == 200>
Upvotes: 0
Views: 952
Reputation: 4992
This won't work great I'm afraid. This is because when you add views to a stack view, the stack view will try to sort the constraints on itself. Based on the intrinsic content size of the views you added. I would probably not use a stack view if you need to update the web view content size.
Upvotes: 1