简体   繁体   English

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

[英]NativeScript: Way to disable zoom controls for iOS WebView?

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. 我正在尝试找出一种方法来防止用户通过捏合手势和双击来在iOS WebView(tns-ios 3.4.1)上进行放大和缩小,本质上是禁用所有缩放功能,例如在苹果进入苹果之前使用的html meta标签让用户决定是否要使用iOS 10及更高版本进行缩放。 I found a solution for android here , for iOS it doesn't appear to be as trivial though. 我在这里找到了适用于android的解决方案,对于iOS,它似乎并不那么琐碎。

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)? 我发现NS最近从UIWebView切换到WKWebView,我们可以通过NativeScript(* with angular)来使用allowMagnification属性吗?

I was able to from NS 3.3 - 4.1 我能够从NS 3.3-4.1

Get your #webview reference and then setup these properties for ios and android to fix the view zooming. 获取您的#webview参考,然后为ios和android设置这些属性以修复视图缩放。

let webView: WebView = this.webView.nativeElement;
webView.on(WebView.loadStartedEvent, function (args: LoadEventData) {               
    if (webView.android) {
        webView.android.getSettings().setBuiltInZoomControls(false);
        webView.android.getSettings().setDisplayZoomControls(false);
    } else {
        webView.ios.scrollView.minimumZoomScale = 1.0;
        webView.ios.scrollView.maximumZoomScale = 1.0;
        webView.ios.scalesPageToFit = false;
        webView.ios.scrollView.bounces = false;
    }
});

No, you will not be able to use allowsMagnification . 不,您将无法使用allowsMagnification You will have extend to your own version of WebView component in order to update meta configuration to stop zooming. 您将扩展到自己的WebView组件版本,以更新元配置以停止缩放。

Update: 更新:

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. 为了禁用缩放,必须修改从{N}核心模块(v5.x)注入的默认视口,这是完成的操作。

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
    });
};

Playground Sample 游乐场样本

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM