Reputation: 227
I have an embedded webView at the bottom of a screen, which loads the Facebook comments javascript plugin.
I want this webView to be seamlessly integrated in the screen, without forcing the user to scroll its contents.
However, when the user taps on some of the webView buttons, the UI height changes, making the webView scrollable.
How do I dynamically update the webView height constraint based on its dynamic contents?
Upvotes: 2
Views: 3434
Reputation: 3956
Yes you can do that. Set height constraint for you web view in storyboard and then inside webviewDidFinishLoad do following
webView.scrollView.scrollEnabled=false;
heightConstraint.constant = webView.scrollView.contentSize.height
Where heightConstraint
is IBOutlet of web view's height constraint. Call setNeedsLayout() after modifying constraint if needed. Also make sure that bottom constraint for web view is set equal to bottom of scroll view so that it expands freely.
UPDATE You can add key value observer for contentSize for your webview's scroll view. You can do it in this way
var myContext = 0
webView.scrollView.addObserver(self, forKeyPath: "contentSize", options: .New, context: &myContext)
override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
if context == &myContext {
heightConstraint.constant = webView.scrollView.contentSize.height
//call layout update if needed
} else {
super.observeValueForKeyPath(keyPath, ofObject: object, change: change, context: context)
}
}
Upvotes: 7