菜鸟教程小白 发表于 2022-12-11 19:31:45

ios - Hybrid Webview IOS 停止缩放


                                            <p><p>我正在使用 Xamarin Forms 构建一个应用程序,在此我实现了一个混合 webview。在 IOS 中,混合 WebView 具有缩放功能,我需要禁用它。</p>

<p>这是我迄今为止尝试过的:-</p>

<pre><code>public class ActivityHybridWebViewRenderer : ViewRenderer&lt;ActivityHybridWebView, WKWebView&gt;, IWKScriptMessageHandler
    {
      const string JavaScriptFunction = &#34;function invokeCSharpAction(data){window.webkit.messageHandlers.invokeAction.postMessage(data);}&#34;;
      WKUserContentController userController;

      protected override void OnElementChanged(ElementChangedEventArgs&lt;ActivityHybridWebView&gt; 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, &#34;invokeAction&#34;);
                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(&#34;invokeAction&#34;);
                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(&#34;file://&#34; + url), new NSUrl(&#34;file://&#34; + Path.GetDirectoryName(url)));
                Task.Run(() =&gt;
                {
                  Task.WaitAny(Task.Delay(2000));
                  InjectJS(JavaScriptFunction);
                  CallJS();
                });

            }
      }
</code></pre>

<p>这两行好像没有效果-</p>

<pre><code>Control.ScrollView.MultipleTouchEnabled = false;
Control.ScrollView.Bounces = false;

webView.ScrollView.MultipleTouchEnabled = false;
webView.MultipleTouchEnabled = false;
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>您可以更改网站代码吗?如果是,您可以通过在 html 头中添加以下标签来禁用缩放:</p>

<pre><code>&lt;meta name=&#34;viewport&#34; content=&#34;width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no&#34; /&gt;
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - Hybrid Webview IOS 停止缩放,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/43510734/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/43510734/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - Hybrid Webview IOS 停止缩放