10

I'm trying to find a way to detect how zoomed in someone is on a web app so that when they click to pull up a menu, the menu stays the same size regardless of the zoom. To do that, I need to be able to scale the size of the menu appropriately relative to the zoom. Is there a way to do this?

John Green
  • 13,241
  • 3
  • 29
  • 51
Mason
  • 6,893
  • 15
  • 71
  • 115

1 Answers1

6

First, all UIWebView has as a UIScrollView subview. So, in order to get it you could do the following:

for (UIView * v in [_webView subviews]) {

    if ([v isKindOfClass:[UIScrollView class]]) {
        UIScrollView * s = (UIScrollView*)v;
        // use the scrollview
    }

}

After that you can use the UIScrollView to find out the zoom scale. Unfortunately, the property zoomScale doesn't tell us, in this case, what we want, but we can use the contentSize and the frame of the scrollView, as the following:

CGFloat zoomScale = s.contentSize.width / s.frame.size.width;

After that, I suppose you want to tell your javascript code about this zoomScale. You could use the following code:

[_webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"yourJSZoomScaleVar = %f;", zoomScale]];

Hope it helps!

Raphael Petegrosso
  • 3,870
  • 2
  • 24
  • 27
  • Sorry, I know that I was looking for it in the context of an actual Web page. Further research suggests that it will be some derivation of window.innerWidth/screen.width. Will have a chance to look at it further tomorrow. – John Green Jun 24 '11 at 08:12