UIWebView behaves erratically when its size is changed
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.
- 0
- 0
- 0
- 0
- 0