ios - 防止 Angular NativeScript WebView 通过捏缩放进行缩放

标签 ios angular nativescript wkwebview angular2-nativescript

所以我有一个使用 Angular 的 Nativescript 应用程序(NG 5.1.1/Angular 7.x)

我有一个 webview 的 View 。

    @ViewChild("myWebView") webViewRef: ElementRef;

    <WebView class="webview" #myWebView [src]="myURL"></WebView>

在我的 webview.component.ts 里面我有这个。
ngAfterViewInit(): void {
    const webview: WebView = this.webViewRef.nativeElement;

    webview.on(WebView.loadFinishedEvent, (args: LoadEventData) => {
        this.setIndicatorFalse();
        if (webview.ios) {
            webview.ios.scrollView.delegate = UIScrollViewDelegateZ.new();
            webview.ios.scrollView.minimumZoomScale = 1;
            webview.ios.scrollView.maximumZoomScale = 1;
        }
    });
    webview.on(WebView.loadStartedEvent, (args: LoadEventData) => {
        if (webview.android) {
            webview.android.getSettings().setBuiltInZoomControls(false);
            webview.android.getSettings().setDisplayZoomControls(false);
        } else {
            // @ts-ignore
            webview.ios.multipleTouchEnabled = false;
            webview.ios.scalesPageToFit = false;
            webview.ios.scrollView.bounces = false;
            webview.ios.scrollView.showsHorizontalScrollIndicator = true;
            webview.ios.scrollView.showsVerticalScrollIndicator = true;
            webview.ios.opaque = false;
            webview.ios.scrollView.allowsInlineMediaPlayback = true;
            webview.ios.scrollView.mediaPlaybackRequiresUserAction = false;
        }
    });
}

正如你所看到的,我已经尝试了各种方法来让这个 webview 不捏缩放。

我正在覆盖我的 ViewDelegate
webview.ios.scrollView.delegate = UIScrollViewDelegateZ.new();

那个文件在这里
export class UIScrollViewDelegateZ extends NSObject implements UIScrollViewDelegate {

public static ObjCProtocols = [UIScrollViewDelegate];

static new(): UIScrollViewDelegateZ {
    console.log("here we are");

    return <UIScrollViewDelegateZ>super.new();
}

viewForZoomingInScrollView(scrollView: UIScrollView): UIView {
    console.log("viewForZoomingInScrollView");

    return null;
}

scrollViewDidScroll(scrollView: UIScrollView): void {
    console.log("scrollViewDidZoom");

    return null;
}

scrollViewWillBeginZoomingWithView(scrollView: UIScrollView, view: UIView): void {
    console.log("scrollViewWillBeginZoomingWithView   " + scrollView);

    return null;
    }
}

当我加载我的 webview 并捏缩放时,我的控制台日志说:
 CONSOLE LOG file:///app/app/webview/scrollDelegate.js:9:20: here we are
 CONSOLE LOG file:///app/app/webview/scrollDelegate.js:21:20: scrollViewWillBeginZoomingWithView   <WKScrollView: 0x7fb140afe000; baseClass = UIScrollView; frame = (0 0; 375 603); clipsToBounds = YES; gestureRecognizers = <NSArray: 0x600000ab2100>; layer = <CALayer: 0x600000537480>; contentOffset: {0, 0}; contentSize: {375, 1916}; adjustedContentInset: {0, 0, 0, 0}>
 CONSOLE LOG file:///app/app/webview/scrollDelegate.js:17:20: scrollViewDidZoom

所以我觉得我很亲近??但我就是不能让它去?我以为我应该在尝试捏缩放时返回 null/undefined?这是其他一些 SO 答案/网络所说的,但没有任何效果。

HTML 页面也有正确的元标记标题...
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0, minimal-ui"/>

任何帮助,将不胜感激!谢谢!!

最佳答案

您必须覆盖 tns-core-modules 设置的默认元数据。

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 ,由于您使用的是 Angular,您可以将以上行添加到您的 app.component.ts

关于ios - 防止 Angular NativeScript WebView 通过捏缩放进行缩放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54452078/

相关文章:

angular - 如何在 Angular 6 中构建具有多个组件的表单?

javascript - 试图了解 Angular 5 的范围。然后

ios - Nativescript - 为 Nativescript 插件设置目标 IOS 版本

angular - 类型 'Observable<>' 在 Angular Nativescript 中缺少属性

ios - Swift - 在自定义 TableViewController 中使用 SearchBar 时遇到问题

ios - UIImageViews/向 View 添加多个贴纸。

ios - 查看 Controller 问题

Iphone 应用程序升级检查

angular - 为什么在 DI 中使用 `deps` 属性

javascript - 内容丰富的 Javascript SDK 和 NativeScript