禁用WKWebView的缩放? [英] Disable zoom in WKWebView?

查看:773
本文介绍了禁用WKWebView的缩放?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道禁用WKWebView中的双击和缩小缩放的简单方法吗?我没有尝试过的作品:

Does anyone know a nice simple way to disable double-click and pinch zooming in a WKWebView? Nothing I've tried works:

Webview.scrollView.allowsMagnification = false;    // Error: value of type WKWebView has no member allowsMagnification

Webview.scrollView.isMultipleTouchEnabled = false; // doesn't do anything

在html中:

<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" /> // pile of crap, does nothing

推荐答案

您将不得不在脚本中添加最大比例.

You will have to add maximum scale in script.

以下代码应为您提供帮助:

The following code should help you:

let source: String = "var meta = document.createElement('meta');" +
    "meta.name = 'viewport';" +
    "meta.content = 'width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no';" +
    "var head = document.getElementsByTagName('head')[0];" +
    "head.appendChild(meta);"

let script: WKUserScript = WKUserScript(source: source, injectionTime: .atDocumentEnd, forMainFrameOnly: true)
let userContentController: WKUserContentController = WKUserContentController()
let conf = WKWebViewConfiguration()
conf.userContentController = userContentController
userContentController.addUserScript(script)
let webView = WKWebView(frame: CGRect.zero, configuration: conf)

这篇关于禁用WKWebView的缩放?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆