问题
I have a UIWebView that, despite having scalesPageToFit set to YES, doesn't perform any such scaling operation: The web page starts off zoomed in and it's necessary to unpinch to get the whole thing on the page.
What could prevent UIWebView::scalesPageToFit from being honored?
回答1:
You could inject the viewport element via JavaScript.
Look for the viewport
meta tag here: Safari Meta Tags
<meta name="viewport" content="width=320, initial-scale=2.3, user-scalable=no">
回答2:
Create a file in your project named changePageWidth.js:
function changePageWidth(width) {
var element = document.createElement('meta');
element.name = "viewport";
element.content = "width=" + width;
var head = document.getElementsByTagName('head')[0];
head.appendChild(element);
}
and then add this code in webViewDidFinishLoad: of UIWebView's delegate:
- (void)webViewDidFinishLoad:(UIWebView *)webView {
NSString *path = [[NSBundle mainBundle] pathForResource:@"changePageWidth" ofType:@"js"];
NSString *jsCode = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
[webView stringByEvaluatingJavaScriptFromString:jsCode];
NSString *jsCall = [NSString stringWithFormat:@"changePageWidth(%d)", webView.frame.size.width];
[webView stringByEvaluatingJavaScriptFromString:jsCall];
}
回答3:
May be you can use java script to resize your browser
NSString *jsCommand = [NSString stringWithFormat:@"document.body.style.zoom = 1.5;"];
webLookupView stringByEvaluatingJavaScriptFromString:jsCommand];
来源:https://stackoverflow.com/questions/9434590/uiwebview-scalespagetofit-wont-scale-page