How to disable scrolling entirely in a WKWebView?

前端 未结 10 1691
再見小時候
再見小時候 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 14:57

    Credit and many thanks to apouche for the Obj-C code. In case anybody else has the same problem, here is the working solution adapted for Swift 2

    extension WKWebView {
    
      func setScrollEnabled(enabled: Bool) {
        self.scrollView.scrollEnabled = enabled
        self.scrollView.panGestureRecognizer.enabled = enabled
        self.scrollView.bounces = enabled
    
        for subview in self.subviews {
            if let subview = subview as? UIScrollView {
                subview.scrollEnabled = enabled
                subview.bounces = enabled
                subview.panGestureRecognizer.enabled = enabled
            }
    
            for subScrollView in subview.subviews {
                if subScrollView.dynamicType == NSClassFromString("WKContentView")! {
                    for gesture in subScrollView.gestureRecognizers! {
                        subScrollView.removeGestureRecognizer(gesture)
                    }
                }
            }
        }
      }
    }
    

提交回复
热议问题