【问题标题】:UIWebView scalesPageToFit won't scale pageUIWebView scalesPageToFit 不会缩放页面
【发布时间】:2012-02-24 16:41:24
【问题描述】:

我有一个 UIWebView,尽管将 scalesPageToFit 设置为 YES,但它不执行任何此类缩放操作:网页开始放大,必须松开手指才能将整个内容显示在页面上。

什么会阻止 UIWebView::scalesPageToFit 被兑现?

【问题讨论】:

  • 如何设置这个 UIWebView?你是在加载页面之前还是之后设置 scalesPageToFit?
  • 我在初始化UIWebView后立即在父视图的ViewDidLoad方法中设置了scalePageToFit。

标签: iphone uiwebview


【解决方案1】:

您可以通过 JavaScript 注入视口元素。

在此处查找viewport 元标记:Safari Meta Tags

<meta name="viewport" content="width=320, initial-scale=2.3, user-scalable=no">

【讨论】:

  • 还有我的一天 :) :)
【解决方案2】:

在您的项目中创建一个名为 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);
}

然后在 UIWebView 的委托的 webViewDidFinishLoad: 中添加这段代码:

- (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】:

    也许你可以使用java脚本来调整你的浏览器大小

    NSString *jsCommand = [NSString stringWithFormat:@"document.body.style.zoom = 1.5;"];
    webLookupView stringByEvaluatingJavaScriptFromString:jsCommand];
    

    【讨论】:

      猜你喜欢
      • 2011-01-21
      • 1970-01-01
      • 2013-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-03
      • 2014-12-05
      相关资源
      最近更新 更多