Disable swipe back gesture in Swift

后端 未结 10 1490
轻奢々
轻奢々 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:13

    The following is an easy approach to disabling & re-enabling the swipe back.

    Swift 3.x & up

    In a viewDidLoad/willAppear/didAppear method add:

    navigationController?.interactivePopGestureRecognizer?.isEnabled = false
    

    Just keep in mind that if you do it with viewDidLoad, then the next time you open the view, it may not be set depending upon whether or not it remains in your stack.

    Unless you want it to remain off, you will need to turn it back on when the view is closed via either willMove(toParentViewController:) or willDisappear. Your navigationController will be nil at viewDidDisappear, so that is too late.

    navigationController?.interactivePopGestureRecognizer?.isEnabled = true
    

    A special note on SplitViewControllers:

    As pointed out by CompC in the comments, you will need to call the second navigation controller to apply it to a detail view as such:

    navigationController?.navigationController?.interactivePopGe‌​stureRecognizer?.isE‌​nabled = false
    

    Swift 2.2 & Objective-C

    Swift versions 2.x & below:

    navigationController?.interactivePopGestureRecognizer?.enabled
    

    Objective-C:

    self.navigationController.interactivePopGestureRecognizer.enabled
    

提交回复
热议问题