NativeScript:禁用 iOS WebView 缩放控件的方法?

问题描述:

我试图找出一种方法来防止用户通过捏合手势在 iOS WebView (tns-ios 3.4.1) 上放大和缩小.双击,基本上禁用所有缩放,就像在苹果让用户决定是否要使用 iOS 10 及更高版本进行缩放之前使用的 html 元标记一样.我在这里找到了一个解决方案,对于不过,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
    });
};

游乐场示例