Jonah
Jonah

Reputation: 4800

UIWebview won't zoom even after setting scalesPageToFit to YES

I have a UIWebview that is loaded as a subview when a user selects a tab on a UISegmentedControl. For some reason, I can not get it to allow pinch/zooming. I set the following code in the viewDidLoad: method, so it should work.

self.myWebView = [[[UIWebView alloc] initWithFrame:self.view.frame] autorelease];
self.myWebView.backgroundColor = [UIColor whiteColor];
self.myWebView.scalesPageToFit = YES;
self.myWebView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
self.myWebView.delegate = self;
[self.view addSubview: myWebView];

I tried loading a UIWebView from a NIB and creating it programmatically with no avail. Is there something I'm missing? What could be causing the webview to ignore pinching and zooming?

Thanks!

Upvotes: 8

Views: 27215

Answers (7)

Vlad
Vlad

Reputation: 3366

This is what Apple suggests in the webview class reference.

Important: You should not embed UIWebView or UITableView objects in UIScrollView objects. If you do so, unexpected behavior can result because touch events for the two objects can be mixed up and wrongly handled.

Upvotes: 1

Michael Nguyen
Michael Nguyen

Reputation: 149

FYI:

<meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.5; user-scalable=1"/>

works but

<meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.5; user-scalable=YES"/>

does not on the iPhone. The documentation says to use yes/no. I guess case matters in this case. In obj-c the values are YES/NO and 1/0

Upvotes: 2

Chris
Chris

Reputation: 63

In my experience, you need to set scalesPageToFit before the UIWebView loads. This means setting before viewDidLoad etc. What I do is set it in "shouldStartLoadWithRequest"

- (BOOL)webView:(UIWebView *)theWebView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    //make sure that the page scales when it is loaded :-)
    theWebView.scalesPageToFit = YES;
    return YES;
}

My interpretation of the documentation is that the scalesPageToFit property dictates how the page WILL be loaded. It does not alter things after the fact.

Hope this helps.

Upvotes: 4

srgtuszy
srgtuszy

Reputation: 1548

I solved this with setting a view port:

<meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.5; user-scalable=1"/>

Good luck

Upvotes: 7

Vaibhav Saran
Vaibhav Saran

Reputation: 12908

  [webView stringByEvaluatingJavaScriptFromString:@"document.body.style.zoom = 5.0;"];

seem to be the suitable solution

Upvotes: 9

Martin Britz
Martin Britz

Reputation: 11

You have to enable multi-touch. Pinch involves more than one finger on the screen:

[myWebView setMultipleTouchEnabled:YES]

Upvotes: 1

Stefan Arentz
Stefan Arentz

Reputation: 34945

I see you are setting the autoresizingMask. Does that mean you have created the UIWebView with an initial size of CGRectZero ? Can you interact with the document at all? I mean, does scrolling/tapping work?

Upvotes: 6

Related Questions