How to determine the scaling scale of a UIWebView

I want to increase the height of the UIWebView while the user zooms in (for example, the default email application on the iPhone). So, I tried the code below to find scrollview in webview and add UIScrollViewDelegate to use scrollViewDidZoom to scale out to increase the height of the scrollViewDidZoom by 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 that I cannot zoom in / out in the scrollViewDisEndZooming but NSLog on scrollViewDisEndZooming showed

scale 1.0

when i finished scaling and scrollViewDidZoom didn't show anything. I want to determine the zoom scale of a webview to calculate its height on scrollViewDidZoom .

What have I done wrong? Please, help.

Thanks in advance.

+4
source share
2 answers

I did well for your code, I will make the following modifications:

you just get the point of the scrollview class, then you can get scrollview.contentSize.width, which may change as you zoom in, but scrollview.zoomScale always remains 1. You also need to use contensize.width to calculate the 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); } 
+5
source

I do not think that there is a correct way to do this from open APIs, but there is a hacker way.

A UIWebView contains a UIScrollView to which a delegate can be provided. Then, when you zoom, you (the delegate) receive a callback to scrollViewDidZoom:

To find the scroll, you can get a collection of subviews from the web view and subviews over it to see which one isKindOfClass:[UIScrollView class] .

UIWebView now also matches UIScrollViewDelegate . This may mean that you are changing the implementation of the web view, and this may be a bad idea. Test well.

0
source

Source: https://habr.com/ru/post/951642/


All Articles