Andre
Andre

Reputation: 4487

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

Upvotes: 9

Views: 5494

Answers (5)

ChrisOSX
ChrisOSX

Reputation: 744

Adapting from Captnwalker1's answer, I came up with this:

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    if(toInterfaceOrientation ==UIInterfaceOrientationPortrait||toInterfaceOrientation==UIInterfaceOrientationMaskPortraitUpsideDown)
    {
        currentScrollView = [[webView subviews] objectAtIndex:0];
        [currentScrollView zoomToRect:CGRectMake(0, 0, currentScrollView.contentSize.width, currentScrollView.contentSize.height) animated:NO];
    }
    else
    {
        currentScrollView = [[webView subviews] objectAtIndex:0];
        [currentScrollView zoomToRect:CGRectMake(0, 0, currentScrollView.contentSize.width, currentScrollView.contentSize.height) animated:NO];
    }
}

So load your webview image, and the image will reset it's size when rotated.

Upvotes: 0

thierryb
thierryb

Reputation: 3728

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

[webView.scrollView setZoomScale:1.0];

Upvotes: 3

HotFudgeSunday
HotFudgeSunday

Reputation: 1403

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.

Upvotes: 1

Captnwalker1
Captnwalker1

Reputation: 700

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];

Upvotes: 1

DarkDust
DarkDust

Reputation: 92316

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];
}

Upvotes: 8

Related Questions