How to disable scrolling entirely in a WKWebView?

前端 未结 10 1719
再見小時候
再見小時候 2020-12-28 14:07

I know this looks like a simple question one can simply say:

webview.scrollView.scrollEnabled = NO;
webview.scrollView.panGestureRecognizer.enabled = NO;
web         


        
10条回答
  •  醉话见心
    2020-12-28 15:07

    Here is a C# extension for WKWebView based on alain.s's swift solution (based on apouche's solution) for those of us using Xamarin. I am using this in my app.

    Notable differences is that I check if subviews exist before looping and instead of dynamically looking for a "WKContentView" (something I'm not sure is even possible in Xamarin) I simply check if each subview has GestureRecognizers and remove them. This will obviously disable all types of gestures so consider this if you expect any user interaction with the web content.

    public static class WKWebViewExtension
        {
            public static void DisableScroll(this WebKit.WKWebView webView)
            {
                webView.ScrollView.ScrollEnabled = false;
                webView.ScrollView.PanGestureRecognizer.Enabled = false;
                webView.ScrollView.Bounces = false;
    
                if (webView.Subviews != null)
                {
                    foreach (var subView in webView.Subviews)
                    {
                        if (subView is UIScrollView)
                        {
                            UIScrollView subScrollView = (UIScrollView)subView;
    
                            subScrollView.ScrollEnabled = false;
                            subScrollView.Bounces = false;
                            subScrollView.PanGestureRecognizer.Enabled = false;
                        }
    
                        if (subView.Subviews != null)
                        {
                            foreach (var subScrollView in subView.Subviews)
                            {
                                if (subScrollView.GestureRecognizers != null)
                                {
                                    foreach (var gesture in subScrollView.GestureRecognizers)
                                    {
                                        subScrollView.RemoveGestureRecognizer(gesture);
                                    }
                                }
                            }
                        }
    
    
                    }
                }
            }
        }
    

提交回复
热议问题