Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reset UIWebView's zoom? I'm already using scalesPagesToFit = YES;

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

like image 974
Andre Avatar asked Jul 23 '10 16:07

Andre


4 Answers

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];
}
like image 142
DarkDust Avatar answered Nov 13 '22 15:11

DarkDust


On iOS 5+ you have access to scrollView. Just do:

[webView.scrollView setZoomScale:1.0];

like image 37
thierryb Avatar answered Nov 13 '22 15:11

thierryb


If you want to do it programmatically this is the only way I could find to accomplish it: (specify your own sizes if you wish, i was attempting to zoom out after typing into a form field)

UIScrollView *sv = [[webViewView subviews] objectAtIndex:0];
[sv zoomToRect:CGRectMake(0, 0, sv.contentSize.width, sv.contentSize.height) animated:YES];
like image 26
Captnwalker1 Avatar answered Nov 13 '22 15:11

Captnwalker1


Update: Downscaling wasn't working properly when using

[[[webView subviews] lastObject] setZoomScale:0.25];

The quality of the images being downscaled on the page was awful. Doing:

[[[webView subviews] lastObject] setZoomScale:0.25 animated:YES];

Fixed it. So that last line is the one you could use.

webView was subclassed of a UIWebView which lies on some IB file. I didn't use the Viewport at all. I find that one should pick by either doing this from the Cocoa Touch side or use JS.

I used:

webView.scalesPageToFit = YES;

I wonder if there's a way of resetting the scalesPageToFit.

like image 1
HotFudgeSunday Avatar answered Nov 13 '22 17:11

HotFudgeSunday