Benches Uncategorized

5

UIWebView behaves erratically when its size is changed

apple Verified

Imported from the Apple Support Forums via the Wayback Machine 🔗

I'm developing an iPhone app where I have a view which has an UIWebView as a subview. Due to the nature of the (rest of the) application I'm resizing and rotating the view "manually" in code when the orientation of the device changes by setting the 'bounds' and 'transform' properties of the view appropriately. This works ok.

The only problem is that the UIWebView in this view behaves erratically when its size changes like this. Due to the type of contents of the web page the 'scales page to fit' property of the WebView is off.

By default (if nothing special is done), if the web page was loaded when the UIWebView was narrow (in portrait mode), and then it's widened (to landscape mode), the UIWebView will scale some fonts larger (instead of rearranging the text) which makes the page look horrible. And if the web page was loaded when the UIWebView was wide (in landscape mode) and then narrowed (to portrait mode) the width of the web page will be retained, making it wider than the UIWebView, requiring horizontal scrolling.

If the page is reloaded after the change, then it will start showing it ok. However, it's inconvenient to reload the page every time the device is rotated (because loading the web page from the internet every time can be slow), and should be unnecessary.

If I perform a small kludge, it provides a partial solution. Namely: After the rotation is done, I programmatically make the UIWebView one pixel larger in both directions and then back to its original size. This will cause it to rearrange the contents if the page was loaded when the UIWebView was narrow and then widened, and the page will look ok. However, for some reason this kludge doesn't work in the opposite situation (ie. the web page was loaded when the UIWebView was wide and then narrowed).

If the following line is added to the web page itself, it also provides a partial solution:

<meta name='viewport' content='width=device-width; initial-scale=1.0; maximum-scale=1.0;'>

Curiously, it works in the exact opposite situation than the resize-by-1-pixel trick: If the page was loaded while in landscape mode and then the device is reoriented to portrait, the contents will be rearranged ok, but not the other way around. (Although this solution is only temporary: If you load in landscape and then reorient to portrait and then back to landscape, the contents will not look the same, but instead the UIWebView will once again scale fonts in strange ways.)

If
both of these tricks are used +at the same time+, then I'm getting what I want: The contents will always look good regardless of when the contents were loaded and how the device is reoriented, without having to reload every time the orientation changes.

However, all this is a big pile of kludges, and it certainly does not feel like the
correct solution.

What is the correct solution? One would think UIWebView could do this automatically or, if not, there would at least be some method that one can call to make it rearrange its contents appropriately (exactly as if the web page was reloaded, but without actually reloading it). However, I can't find any such method.

5 replies

Sign in or join Beachball Help to reply.

  1. 0

    mcorner

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    I am really curious as to wether anyone found a solution to this. Essentially, for a web page that is designed to zoom on rotation, if you load a webview in landscape the fonts are small, but if you load in portrait then rotate to landscape they are large.

    We pull a similar resizing trick, but every once in a while there is a timing error of some sort and we end up with a rotated webview that is the wrong size. IT is surprising seeing what a hack this is.

    I am wondering if there is a javascript method one can add to trigger the webview redisplaying?

  2. 0

    mcBontempi

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    I'm looking for an answer to this issue also.

  3. 0

    spymaster

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Me too. It's pretty annoying.

    If the app starts in portrait orientation, then rotated to landscape, setting the frame size causes the web content to re-layout to the new view size.

    If the app starts in landscape orientation, then rotated to portrait, setting the frame size doesn't.

    Inconsistent behaviour.

    A view that doesn't re-layout the content when getting smaller.

    Dammnit.

  4. 0

    Agent Gold

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    One way I've found to handle this is to reload the content after the resize. When I say "reload," I don't mean the built-in reload function on UIWebView, I mean the actual call to loadHTMLString: baseURL: (or whichever method you use to load your content initially).

    This brings an obvious performance hit (cache your content ahead of time if you can), and it also brings a few other behavioral oddities (you will lose your place in terms of scrolling). The good news is that your content will all be there and will look how you expect it to. Depending on what you're working on, this might be acceptable or it might be too much of a trade-off; you'll have to test that much for yourself. I discovered this working with programmatically-created HTML, so it was just a matter of caching and reloading the result.

    If the resize is taking place because of device rotation, you can add your reload code to the willRotateToInterfaceOrientation: duration: method. It may reformat your text briefly, but will correct itself quickly.

    Hopefully that will help at least a few people who are experiencing this problem (or at least point someone in the direction of a better solution).

  5. 0

    spymaster

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Hi.

    I found a solution posted somewhere (can't remember where).

    Bascially, to trigger the UIWebView to resize down from landscape to portrait on rotation, just add this meta tag to the HTML content within the head element:

    <meta name='viewport' content='width=device-width; initial-scale=1.0; maximum-scale=1.0;'>

    Magic!