8

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!

Paras Joshi
  • 20,427
  • 11
  • 57
  • 70
Jonah
  • 4,810
  • 14
  • 63
  • 76
  • `scalesPageToFit = YES` it works but it change your default content size, [Look at my answer this will helpful for you.](http://stackoverflow.com/questions/7134576/enable-zooming-pinch-on-uiwebview/23971234#23971234) – iPatel Jul 22 '14 at 03:19

7 Answers7

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

seem to be the suitable solution

Vaibhav Saran
  • 12,848
  • 3
  • 65
  • 75
7

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

srgtuszy
  • 1,548
  • 1
  • 18
  • 16
6

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?

Stefan Arentz
  • 34,311
  • 8
  • 67
  • 88
  • I added a piece of the code that I previously left out. I've tried removing the autoresizingMask part with no difference. Yes, everything else works perfectly. – Jonah Feb 28 '10 at 14:07
  • What about trying a different web page? To rule out that the page you are testing with has some configuration to disallow pinch/zoom. (This can be influenced by the page) – Stefan Arentz Feb 28 '10 at 14:43
  • You are correct. I was only loading wikipedia pages and for some reason they don't allow zooming. Any idea why they don't? Thanks! – Jonah Feb 28 '10 at 14:51
  • 1
    Not totally sure. I think it is done with the `viewport` meta data .. check http://developer.apple.com/safari/library/documentation/AppleApplications/Reference/SafariHTMLRef/Articles/MetaTags.html#//apple_ref/doc/uid/TP40008193-SW1 – Stefan Arentz Feb 28 '10 at 19:13
4

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.

Chris
  • 63
  • 1
  • 2
2

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

Michael Nguyen
  • 149
  • 3
  • 12
1

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

[myWebView setMultipleTouchEnabled:YES]

  • That was not the problem. Multitouch is enabled by default. There seems to be some sort of code in the wikipedia mobile website that prevented it from zooming in. It works fine on all other sites. – Jonah Oct 11 '10 at 15:34
1

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.

Vlad
  • 3,346
  • 2
  • 27
  • 39