NativeScript:如何禁用iOS WebView的缩放控件?

问题描述:

我正在尝试找出一种方法,以防止用户通过捏合手势&来放大和缩小iOS WebView(tns-ios 3.4.1).双击,本质上会禁用所有缩放功能,例如在苹果进入用户之前决定是否要使用iOS 10及更高版本进行缩放的html meta标签. 我在此处中找到了一个解决方案,用于在iOS上,它似乎并不那么琐碎.

I am trying to figure out a way to prevent users from zooming in and out on an iOS WebView (tns-ios 3.4.1) via pinch gestures & double tapping, essentially disabling all scaling like the html meta tag used to do before apple went to letting the user decide if he wants to zoom with iOS 10 and above. I found a solution for android here, for iOS it doesn't appear to be as trivial though.

我对这个平台还很陌生,目前可以做到这一点吗?我发现NS最近从UIWebView切换到WKWebView,我们可以使用 allowsMagnification 以某种方式来自NativeScript的属性(带角的*)?

I am pretty new to the platform, is this currently possible at all? I found that NS recently switched from UIWebView to WKWebView, can we use the allowsMagnification property somehow from NativeScript (*with angular)?

否,您将无法使用allowsMagnification.您将扩展到自己的WebView组件版本,以便更新元配置以停止缩放.

No, you will not be able to use allowsMagnification. You will have extend to your own version of WebView component in order to update meta configuration to stop zooming.

更新:

从{N}核心模块(v5.x)注入的默认视口必须进行修改以禁用缩放,这是这样做的.

The default viewport being injected from {N} core module (v5.x) has to be modified in order to disable zoom, here is how it done.

import { WebView } from 'tns-core-modules/ui/web-view';

declare var WKUserScript, WKUserScriptInjectionTime, WKUserContentController, WKWebViewConfiguration, WKWebView, CGRectZero;

(<any>WebView.prototype).createNativeView = function () {
    const jScript = `var meta = document.createElement('meta'); 
    meta.setAttribute('name', 'viewport');
    meta.setAttribute('content', 'initial-scale=1.0 maximum-scale=1.0');
    document.getElementsByTagName('head')[0].appendChild(meta);`;
    const wkUScript = WKUserScript.alloc().initWithSourceInjectionTimeForMainFrameOnly(jScript, WKUserScriptInjectionTime.AtDocumentEnd, true);
    const wkUController = WKUserContentController.new();
    wkUController.addUserScript(wkUScript);
    const configuration = WKWebViewConfiguration.new();
    configuration.userContentController = wkUController;
    configuration.preferences.setValueForKey(
        true,
        "allowFileAccessFromFileURLs"
    );
    return new WKWebView({
        frame: CGRectZero,
        configuration: configuration
    });
};

游乐场样本