UIWebView scalesPageToFit won't scale page

ぃ、小莉子 提交于 2019-12-22 18:02:13

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!