views:

234

answers:

1

Hi guys,

I've been looking for the past week for the answer to this question.

I have a UIWebView, inside of a UIScrollView. Everything works great, but I want the content of the UIWebView to reset its zoom, when the orientation changes.

In the HTML inside the UIWebView, I set the width of the viewport (w/ a meta tag) to "device-width" and then on the Obj-C side, I set the scalesPagesToFit = YES;

I've tried resetting the zoom with javascript; by replacing the meta tags in runtime; reloading; accessing the UIScrollView inside of the UIWebView; etc...

but with no success.

Any of you gods know a workaround?

The only one I can think off is to recreate the UIWebViews every time we change the orientation, but that makes them flash to white whilst rendering content, which looks terrible :(

Any thoughts?

Many thanks, Andre

+2  A: 

I'm just guessing here and haven't tried, but AFAIK a UIWebView has a UIScrollView child. So one should be able to do:

for (UIScrollView *scroll in [myWebView subviews]) {
    // Make sure it really is a scroll view and reset the zoom scale.
    if ([scroll respondsToSelector:@selector(setZoomScale:)])
        [scroll setZoomScale:1.0];
}
DarkDust
Do you know if this would pass the App Store's approval, or is it a private API? Cheers, Andre
Andre
This should be perfectly fine. I've seen an article that uses the same technique to switch off the bouncing of the view when it reaches the end. Only public APIs are used here so nothing for Apple to complain about. If Apple decides to change UIWebView to not use a UIScrollView child any more this code won't crash, it just won't reset the zoom any more.
DarkDust
Cool. Just so you know, I'd tried this code before and it didn't work. I've found that to make it work (in my case) you need to first replace the meta tags in the HTML, to change the viewport's maximum-scale back to 1.0, and then call the setZoomScale.
Andre