Disable swipe back gesture in Swift

后端 未结 10 1451
轻奢々
轻奢々 2020-12-02 09:47

Been looking around on here for a while but can\'t seem to find a working solution.

I\'m trying to disable the swipe to go back to previous view gesture, in Swift.

相关标签:
10条回答
  • 2020-12-02 10:23

    RowanPD's logic for Swift 4

    private var popGesture: UIGestureRecognizer?
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
    
        if navigationController!.responds(to: #selector(getter: UINavigationController.interactivePopGestureRecognizer)) {
            self.popGesture = navigationController!.interactivePopGestureRecognizer
            self.navigationController!.view.removeGestureRecognizer(navigationController!.interactivePopGestureRecognizer!)
        }
    
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
    
        if let gesture = self.popGesture {
            self.navigationController!.view.addGestureRecognizer(gesture)
        }
    
    }
    
    0 讨论(0)
  • 2020-12-02 10:25

    If requirement is to show side menu on some of the screens then add AddScreenEdgePanGesture on this specific view instead of navigationController view

    replace it

    SideMenuManager.default.menuAddScreenEdgePanGesturesToPresent(toView: self.navigationController?.view)
    

    with this

    SideMenuManager.default.menuAddScreenEdgePanGesturesToPresent(toView: self.view)
    
    0 讨论(0)
  • 2020-12-02 10:28

    This is something you missed if it doesn't work after you tried all.

    1. Add navigationController?.interactivePopGestureRecognizer?.isEnabled = false to your viewWillAppear(animated:) method.
    2. if it doesn't work, remove navigation delegate from the view controller. Check again if your view controller is confirming UINavigationControllerDelegate, UIGestureRecognizerDelegate protocols. if so, just remove it.
    0 讨论(0)
  • 2020-12-02 10:29

    I was able to do this by returning false in gestureRecognizerShouldBegin

    class ViewController2: UIViewController, UIGestureRecognizerDelegate {
    ...
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        self.navigationController?.interactivePopGestureRecognizer.delegate = self
    }
    
    func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer) -> Bool {
        return false
    }
    
    0 讨论(0)
提交回复
热议问题