Disable swipe back gesture in Swift

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

Leave a Comment