ScalesPageToFit broken on UIWebView after keyboard?

I have this meta tag:

<meta name="viewport" content="width=768, minimum-scale=1.0, user-scalable=no" /> 

And I set scalesPageToFit:

 webView.scalesPageToFit = YES; 

At first it works correctly. When I switch from portrait to landscape, it increases, so the width of the device is always 768 pixels. I can go back and forth without problems (portrait and landscape).

As soon as I activate the virtual keyboard, whether through the contentEditable or text area, it stops working completely, even after I turn off the keyboard.

Is this a bug in UIWebView? Is there anything else I need to do to make this work? Can i work Width, regardless of orientation, should be 768px.

+4
source share
2 answers

This seems to be a bug with the Apple UIDocumentView private class. viewportConfigurationsDidChange stuck on the zoom scale after activating the keyboard, which is set when _setDocumentScale called. For example, when it should go to 1.333, it is stuck at 1.0.

It is "fixed" by writing a category for UIWebDocumentView, which is a subclass of UIDocumentView that does not implement these methods (so you can call super).

Despite all my efforts, I could not find a way to fix this using the open API. I tried messing around with the zoom level of UIScrollView, but I was never able to get this to work correctly. The content will be biased and other strange things will happen. Even tried to do this using JavaScript.

Thus, the only way to fix this is through a category in private classes.

It will never be approved in the app store.

So, until Apple fixes this, SOL.

+1
source

ATTENTION: this solution uses private APIs. Any application using this code may be rejected from the application store. I am sending it so that people have at least a working solution.

 @interface UIWebDocumentView : UIView - (void)_setDocumentScale:(float)scale; @end @interface UIWebView (DocumentView) - (UIWebDocumentView *)_documentView; @end - (void)resetScale:(UIWebView *)webView { UIWebDocumentView *webDocumentView = [webView _documentView]; CGFloat contentWidth = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.scrollWidth;"] floatValue]; CGSize viewSize = webView.bounds.size; float scale = viewSize.width / contentWidth; [webDocumentView _setDocumentScale:scale]; } 

Call resetScale after changing the boundaries of the web view.

0
source

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


All Articles