混合Webview IOS停止缩放

时间:2017-04-20 05:14:10

标签: ios webview xamarin.ios xamarin.forms

我正在使用Xamarin Forms构建应用程序,在此我实现了混合webview。在IOS中,混合Web视图具有缩放功能,我需要对其进行禁用。

这是我到目前为止所尝试的: -

  public class ActivityHybridWebViewRenderer : ViewRenderer<ActivityHybridWebView, WKWebView>, IWKScriptMessageHandler
    {
        const string JavaScriptFunction = "function invokeCSharpAction(data){window.webkit.messageHandlers.invokeAction.postMessage(data);}";
        WKUserContentController userController;

        protected override void OnElementChanged(ElementChangedEventArgs<ActivityHybridWebView> e)
        {
            base.OnElementChanged(e);

            if (Control == null)
            {
                userController = new WKUserContentController();
                var script = new WKUserScript(new NSString(JavaScriptFunction), WKUserScriptInjectionTime.AtDocumentEnd, false);
                userController.AddUserScript(script);
                userController.AddScriptMessageHandler(this, "invokeAction");
                var config = new WKWebViewConfiguration { UserContentController = userController };
                var webView = new WKWebView(Frame, config);
                webView.ScrollView.MultipleTouchEnabled = false;
                webView.ScrollView.Bounces = false;
                SetNativeControl(webView);
            }
            if (e.OldElement != null)
            {
                Control.ScrollView.MultipleTouchEnabled = false;
                Control.ScrollView.Bounces = false;
                userController.RemoveAllUserScripts();
                userController.RemoveScriptMessageHandler("invokeAction");
                var hybridWebView = e.OldElement as ActivityHybridWebView;
                hybridWebView.Cleanup();
            }
            if (e.NewElement != null)
            {
                string localpath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
                string url = Path.Combine(localpath, Element.Uri);
                Control.LoadFileUrl(new NSUrl("file://" + url), new NSUrl("file://" + Path.GetDirectoryName(url)));
                Task.Run(() =>
                {
                    Task.WaitAny(Task.Delay(2000));
                    InjectJS(JavaScriptFunction);
                    CallJS();
                });

            }
        }

这两行似乎没有效果 -

Control.ScrollView.MultipleTouchEnabled = false;
Control.ScrollView.Bounces = false;

webView.ScrollView.MultipleTouchEnabled = false;
webView.MultipleTouchEnabled = false;

2 个答案:

答案 0 :(得分:0)

您可以更改网站代码吗?如果是,则可以通过在html head中添加以下标签来禁用此处的缩放功能:

• Email address: chriscoyier@gmail.com

答案 1 :(得分:0)

Xamarin:在iOS WKWebView中禁用缩放

var source = "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);";

var script = new WKUserScript(new NSString(source), WKUserScriptInjectionTime.AtDocumentEnd, true);

wkWebView.Configuration.UserContentController.AddUserScript(script);
  

来源:https://stackoverflow.com/a/41741125/546054