Iphone – How to detect zoom scale of UIWebView

iphoneuiscrollviewuiwebviewzooming

I want to increase height of UIWebView while user zoom in (like default mail app in iPhone).
So, I have tried the code below to find scrollview in webview and add UIScrollViewDelegate to use scrollViewDidZoom for detecting zoom scale to increase height of webview on this method.

// MessageAppDelegate.h
@interface MessageAppDelegate : NSObject <UIApplicationDelegate,UIWebViewDelegate,UIScrollViewDelegate> {   
    UIWebView *webview;
    UIScrollView *scrollview;
}


//MessageAppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{

    [self.window makeKeyAndVisible];

    webview = [[UIWebView alloc] initWithFrame:CGRectMake(0, 150, 320, 100)];
    webview.delegate = self;
    webview.scalesPageToFit = YES;
    webview.userInteractionEnabled = YES;

    [webview loadHTMLString:@"<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, maximum-scale=6.0 user-scalable=yes\">test test" baseURL:nil];

    [self.window addSubview:webview];

    // Find scrollview in webview
    for (UIView *view in webview.subviews) {
        if ([view isKindOfClass:[UIScrollView class]]) {
            // Get UIScrollView object
            scrollview = (UIScrollView *) view;
            scrollview.delegate = self;
        }
    }

    return YES;
}

- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale{
    NSLog(@"scale %f",scale);
}

- (void)scrollViewDidZoom:(UIScrollView *)scrollView{
    NSLog(@"scrollViewDidZoom %f",scrollview.zoomScale);
}

The problem is I cannot zoom in/out on the webview
but NSLog on scrollViewDisEndZooming method showed

scale 1.0

when I ended zooming
and scrollViewDidZoom method didn't show anything.
I want to detect zoom scale of webview for calculating its height on scrollViewDidZoom.

What I've done wrong?
Please help.

Thanks in advance.

Best Answer

I have done well for your code up,do following modificaitons:

you just get the scrollview class point ,then can get the scrollview.contentSize.width,which can change when you zoom in out,but scrollview.zoomScale always remains 1.so have to use contensize.width to calculate scale.

@interface dictViewController : UIViewController<UIWebViewDelegate,UITextFieldDelegate,UIGestureRecognizerDelegate,UIScrollViewDelegate>{

UIScrollView *scrollview;
......................

//scrollview.delegate = self;

for (UIView *view in webViewM.subviews) {
    if ([view isKindOfClass:[UIScrollView class]]) {
        // Get UIScrollView object
        scrollview = (UIScrollView *) view;
        //scrollview.delegate = self;
    }
}
/*
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale{
//NSLog(@"scale zooming %f",scale);
}

- (void)scrollViewDidZoom:(UIScrollView *)scrollView{
//NSLog(@"scrollViewDidZoom %f",scrollview.zoomScale);
}*/


- (void)swipeRightAction:(id)ignored
{  

 NSLog(@"wid=%f", scrollview.contentSize.width);
 NSLog(@"zoomscale=%f", scrollview.zoomScale);

}