Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable swipe back gesture in Swift

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.

I've tried a variety of solutions including:

self.navigationController?.interactivePopGestureRecognizer.enabled = false 

and

self.navigationController.interactivePopGestureRecognizer.delegate = self  func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer!) -> Bool {     return false } 

Is there a new method of doing this or some other method that works?

like image 327
Phil Hudson Avatar asked Jul 30 '15 18:07

Phil Hudson


People also ask

How do I turn off swipe back in IOS?

EDIT. If you want to manage swipe back feature for specific navigation controllers, consider using SwipeBack. With this, you can set navigationController. swipeBackEnabled = NO .

What is Interactivepopgesturerecognizer?

The gesture recognizer responsible for popping the top view controller off the navigation stack.

How do I turn off swipe back on Iphone in react native?

To make this work, you need to: Disable the swipe gesture for the screen ( gestureEnabled: false ). Override the native back button in the header with a custom back button ( headerLeft: (props) => <CustomBackButton {... props} /> ).


2 Answers

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 
like image 170
CodeBender Avatar answered Sep 21 '22 13:09

CodeBender


You could disable it but that would not be to recommended as most iOS users go back by swiping and less by pressing the back button. If you want to disable it it would be more reasonable to use a modal segue instead of a push segue which is not that big of a transfer. If you really want to get rid of the swipe to go back function I would just disable the back button and have a done button on the top right of the screen.

self.navigationController?.navigationItem.backBarButtonItem?.isEnabled = false; 
like image 33
Stefan DeClerck Avatar answered Sep 21 '22 13:09

Stefan DeClerck