问题
I have UIWebview in storyboard, I am loading HTML pages inside the view. till iOS 9.2 it is working great as desire, but when i am running same in iOS 9.3 the page is appeared with zoom in by default. I have set scalesPageToFit property.
self.webView.scalesPageToFit=TRUE;
I have too tried setting frame programatically for UIWebview.
This are my Meta tag from HTML..
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="language" content="en" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=2.2, user-scalable=yes">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
i have tried with meta tag
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
As suggested in many post i have tried with.
[self.webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.querySelector('meta[name=viewport]').setAttribute('content', 'width=%d;', false); ", (int)self.view.frame.size.width]];
回答1:
I had quite the same issue for me the solution was using this code bellow
webview.scalesPageToFit = YES;
float width = webview.frame.size.width;
float scale = width/770;
NSString *js = [NSString stringWithFormat:@"document.body.style.zoom=%.2f;",scale];
[webview stringByEvaluatingJavaScriptFromString:js];
I am aware of that this is hacky workaround. The number 770 is the ratio between width(points) and scale which I need for fitting in a content of web page to webview.
UPDATE
I found out that above solution time to time cause not correctly scaling letters in specific html tags, that's why I came with javascript, which changes directly meta tag with name viewport (code bellow)
NSString *js = [NSString stringWithFormat:@"document.querySelector('meta[name=viewport]').setAttribute('content','width=device-width; initial-scale=%.2f; user-scalable=no;');",scale];
回答2:
- (BOOL)webView:(UIWebView *)theWebView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
//make sure that the page scales when it is loaded :-)
theWebView.scalesPageToFit = YES;
return YES;
}
seem to be the suitable solution
来源:https://stackoverflow.com/questions/36426007/scalespagetofit-for-webview-is-not-working-with-ios-9-3