In a previous post I described how to programmatically set the height of a UIWebView to fit the height of its HTML content.
This is a different approach using a Storyboard (and a little code). To make things a little bit more interesting I added an UIView that sits on top of the UIWebView and that should scroll out of view when the user scrolls the UIWebView. To make that happen we need an UIScrollView that contains the UIView and the UIWebView:
The UIView could be something like an iAd that you want to display on top of the web content but that should be scrolled out of the view when the user scrolls the web content.
Here is how to make it work:
Connect the UIWebView from the nib to an outlet in your view controller.
Disable scrolling in the UIWebView.
Set the constraints on the `UIScrollView`, the `UIView` and the `UIWebView`:
The UIScrollView needs a top, a bottom, a leading and a trailing constraint to the UIViewController’s view.
The UIView needs a top, a leading and a trailing constraint to the UIScrollView. It also needs a width constraint that is equal to the UIScrollView’s width to avoid horizontal scrolling (See this post for an explaination). I also add a height constraint, because I want to have the UIView to have a constant height of 100pt.
The UIWebView needs a top constraint to the UIView’s bottom, a leading, a trailing and a bottom constraint to the UIScrollview. It also needs a height constraint that we will later set to the height of the HTML content
The constraints should look like this:
Connect the UIWebView‘s height constraint to an outlet in your view controller.
Set the view controller as UIWebViewDelegate.
In webViewDidFinishLoad set the height constraint’s constant to the height of the contentSize of the scroll view inside the web view.
Start Key-Value Observing on the contentSize to change the height, when height of the web view has to change because segments of the webpage change their size without reloading the page (like accordeons, or menus). Don’t forget to stop observing when the view controller gets deallocated.
Recently I encountered a problem with the keyboard while displaying a HTML form inside a UIWebView: The HTML form was a simple form to ask the login credentials from a user. Nothing more than 2 text input fields and a submit button. When the user tapped on an input field the iOS Keyboard came up, as it should. But then, when the user tapped the submit button, the keyboard stayed in placed and was not dismissed. The normal behaviour would be that the keyboard would be dismissed as soon as the textfield loses it’s focus. But somehow this was not happening.
Then I found the solution to that problem. I turned out that the lingering keyboard was a UI design choice by Apple. The keyboard dismissal is disabled when you present your UIWebView using a modal ViewController with presentation style UIModalPresentationFormSheet!
The solution is quite simple: You have to override your ViewController’s disablesAutomaticKeyboardDismissal method:
Sometimes you need to know the height of a html document that is loaded into a UIWebView. For example if you want to set the height of the UIWebView to the height of its content.
The logical way would be this:
This sounds easy, but if you look at the results you’ll realize that the values for the document height are not correct and pretty random.
The problem is: webViewDidFinishLoad: get’s called when the HTML is fully loaded BUT it still has to be fully rendered before you can determine its height!
So the call comes too early. You could delay the call but that’s not the way to go here as this is still unreliable and you want the height as soon as possible.
Here’s how to do it:
Add this function either to the head or the body of your HTML
This function gets called as soon as the HTML document is fully rendered. It puts the height into an URL and sends a request with this URL.
2. Intercept the request in the UIWebView’s delegate
A UIWebViewDelegate has a method, that get’s called everytime the HTML inside the UIWebView sends a request. Here’s what to do in this method:
Return NO to stop the UIWebView from trying to load the request. Don’t forget to return YES for all other requests or the HTML content won’t even get loaded into the UIWebView in the first place.