UIWebView 以低质量的放大状态呈现网页

UIWebView rendering web pages in a low-quality zoomed in state

使用UIWebView 呈现第三方网站。由于各种原因,我无法使用 WKWebView。

故事板中没有 Xib/View 与我的 ViewController 关联。所有 UI 都在 viewDidLoad() 方法中加载

    let navigationBar = UINavigationBar()
    navigationBar.setTranslatesAutoresizingMaskIntoConstraints(false)
    navigationBar.barStyle = UIBarStyle.Black
    //added navigation items
    self.view.addSubview(navigationBar)

    UXHelper.createHorizontalConstraints(navigationBar, outerView: self.view, margin: 0)
    UXHelper.createConstraint(navigationBar, parent: self.view, to: self.view, constraint: NSLayoutAttribute.Top, margin: 0)
    UXHelper.createConstraint(navigationBar, parent: self.view, to: nil, constraint: NSLayoutAttribute.Height, margin: AlgoLinkWebViewController.navBarHeight)

    self.webView = UIWebView()
    self.webView.backgroundColor = UIColor.whiteColor()
    self.webView.opaque = false
    self.webView.setTranslatesAutoresizingMaskIntoConstraints(false)
    self.webView.delegate = self
    self.view.addSubview(self.webView)

    UXHelper.createHorizontalConstraints(self.webView, outerView: self.view, margin: 0)
    UXHelper.createConstraint(self.webView, parent: self.view, to: self.view, constraint: NSLayoutAttribute.Bottom, margin: 0)
    self.view.addConstraint(NSLayoutConstraint(item: self.webView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: navigationBar, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))

使用上面的代码下面是渲染出来的webView的截图

我正在寻找的预期 WebView 渲染是 .这是来自另一个我知道使用 UIWebView 但无权访问代码

的应用程序

注意:请忽略导航栏差异

我试过的选项是

  1. 删除自动调整 = false
    • 删除它并没有删除放大状态
  2. webView.scalesWebPagesToFit = 真
    • 设置这个没有什么不同
  3. 正在使用父视图框架大小初始化 UIWebView
    • self.webView = UIWebView(frame: self.view.bounds) - 没有区别
  4. 将滚动视图比例值设置为 0.5
    • 没有影响

如果你想实现字体大小的变化,你可以使用 Java 脚本通过在你的 viewDidLoad 方法中添加 fallowing 来实现

var fontSize = 104 //Your preferred font size go here
var jsString = "document.getElementsByTagName('body')[0].style.fontSize='\(fontSize)px'"
    webView.stringByEvaluatingJavaScriptFromString(jsString)
webView.stringByEvaluatingJavaScriptFromString(jsString)

希望对您有所帮助!

也许您在 iPhone6/6plus 上使用了缩放模式。