-->

Detect Zoomscale in UIWebView

2020-07-22 06:21发布

问题:

My problem is that i want to detect the zoom scale of an UIWebView, i have tried searching it but did not come out with a proper answer.Any help is appreciated......

回答1:

Well although the UIWebView doesn't have a zoomScale property, UIScrollView does! So we just scan it's subView's for the scrollView everything sits in and get it that way.

Here's a little (1 method) category that will allow you to get the scale by calling [webView zoomScale].

UIWebView+zoom.h file

@interface UIWebView (zoom)
-(float)zoomScale;
@end

UIWebView+zoom.m file

@implementation UIWebView (zoom)

-(float)zoomScale{
    UIScrollView *webViewContentView;
    for (UIView *checkView in [self subviews] ) {
        if ([checkView isKindOfClass:[UIScrollView class]]) {
            webViewContentView = (UIScrollView*)checkView;
            break;
        }
    }
    return webViewContentView.zoomScale;
}

@end

UIScrollView Class Reference

UIWebView's View Hierarchy (Don't rely on it though, always scan the webView to avoid code breaking when apple makes changes to iOS)

NOTE: This code should work but has been written in the reply box so hasn't been tested.



标签: iphone