How to disable scrolling entirely in a WKWebView?

前端 未结 10 1688
再見小時候
再見小時候 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:46

    I found that I had to make my view controller a UIScrollViewDelegate then add this function to prevent scrolling.

    func scrollViewDidScroll(_ scrollView: UIScrollView) {
       scrollView.setContentOffset(CGPoint(x: 0, y: 0), animated: false)
    }
    
    0 讨论(0)
  • 2020-12-28 14:50

    Before Swift 3

    You can simply disable scroll on its implicit scrollView

    webView.scrollView.scrollEnabled = false
    

    Swift 3

    webView.scrollView.isScrollEnabled = false
    
    0 讨论(0)
  • 2020-12-28 14:55

    Try to disable scrollView zoom in this way:

    CGFloat zoomScale = webview.scrollView.zoomScale;
    webview.scrollView.maximumZoomScale = zoomScale;
    webview.scrollView.minimumZoomScale = zoomScale;
    
    0 讨论(0)
  • 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)
                    }
                }
            }
        }
      }
    }
    
    0 讨论(0)
  • 2020-12-28 15:03

    finally self.webView.scrollView.userInteractionEnabled = NO

    0 讨论(0)
  • 2020-12-28 15:03

    Here is a Swift 3 version:

    extension WKWebView {
    
        func setScrollEnabled(enabled: Bool) {
            self.scrollView.isScrollEnabled = enabled
            self.scrollView.panGestureRecognizer.isEnabled = enabled
            self.scrollView.bounces = enabled
    
            for subview in self.subviews {
                if let subview = subview as? UIScrollView {
                    subview.isScrollEnabled = enabled
                    subview.bounces = enabled
                    subview.panGestureRecognizer.isEnabled = enabled
                }
    
                for subScrollView in subview.subviews {
                    if type(of: subScrollView) == NSClassFromString("WKContentView")! {
                        for gesture in subScrollView.gestureRecognizers! {
                            subScrollView.removeGestureRecognizer(gesture)
                        }
                    }
                }
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题