UIWebView disable zooming when scalesPageToFit is ON

血红的双手。 提交于 2019-11-28 23:15:34

This works for me:

UIScrollView *scrollView = [webView.subviews objectAtIndex:0];
scrollView.delegate = self;//self must be UIScrollViewDelegate

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
  return nil;
}

UPDATE: For iOS 5 or higher get scrollView like this:

webview.scrollView.delegate = self;

Instead of using

UIScrollView *scrollView = [webView.subviews objectAtIndex:0];
scrollView.delegate = self;//self must be UIScrollViewDelegate

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return nil;
}

You can use this:

webview.scrollView.delegate = self;

-(UIView*)viewForZoomingInScrollView:(UIScrollView*)scrollView {
return nil;
}

Second option is better and full-proof. First logic could fail in future SDKs.

There's property called scrollView in UIWebView. Through it you can define this. To be more specific it should look like this:

myWebView.scrollView.minimumZoomScale = 1.0;
myWebView.scrollView.maximumZoomScale = 1.0;

Edit: Implement delegate method

- (void)webViewDidFinishLoad:(UIWebView *)webView

For your web view and in it add folowing code:

[webView stringByEvaluatingJavaScriptFromString: @"$('body').bind('touchmove', function(event) { event.preventDefault() });"];

The problem is this also disable the scroll, because it disable the move of touch. If you want to keep the scroll you have to write a java script that will overwrite the viewport meta tag with:

<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
cvursache

From the Apple Documentation:

scalesPageToFit

A Boolean value determining whether the webpage scales to fit the view and the user can change the scale.

@property(nonatomic) BOOL scalesPageToFit

Discussion

If YES, the webpage is scaled to fit and the user can zoom in and zoom out. If NO, user zooming is disabled. The default value is NO.

So if you want to change that behavior, you will probably have to subclass UIWebView.

so it works at me:

NSString *fullURL = @"http://google.com";
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
_webView.scrollView.scrollEnabled = TRUE;
_webView.contentMode = UIViewContentModeScaleAspectFit;
_webView.scalesPageToFit = FALSE;
[_webView loadRequest:requestObj];
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!