Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable back swipe gesture in UINavigationController on iOS 7

In iOS 7 Apple added a new default navigation behavior. You can swipe from the left edge of the screen to go back on the navigation stack. But in my app, this behavior conflicts with my custom left menu. So, is it possible to disable this new gesture in UINavigationController?

like image 803
ArtFeel Avatar asked Jun 20 '13 09:06

ArtFeel


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 .

How do I turn off swipe navigation in Swift?

Tap the gear icon next to “Gesture Navigation.” Note: The corner swipe gesture for Google Assistant is only available if you use gesture navigation. You don't have to turn it off if you use three-button navigation. Simply toggle the switch off for “Swipe to Invoke Assistant.”


2 Answers

I found a solution:

Objective-C:

if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {     self.navigationController.interactivePopGestureRecognizer.enabled = NO; } 

Swift 3+:
self.navigationController?.interactivePopGestureRecognizer?.isEnabled = false

like image 124
ArtFeel Avatar answered Sep 19 '22 09:09

ArtFeel


I found out setting the gesture to disabled only doesn't always work. It does work, but for me it only did after I once used the backgesture. Second time it wouldn't trigger the backgesture.

Fix for me was to delegate the gesture and implement the shouldbegin method to return NO:

- (void)viewDidAppear:(BOOL)animated {     [super viewDidAppear:animated];      // Disable iOS 7 back gesture     if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {         self.navigationController.interactivePopGestureRecognizer.enabled = NO;         self.navigationController.interactivePopGestureRecognizer.delegate = self;     } }  - (void)viewWillDisappear:(BOOL)animated {     [super viewWillDisappear:animated];      // Enable iOS 7 back gesture     if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {         self.navigationController.interactivePopGestureRecognizer.enabled = YES;         self.navigationController.interactivePopGestureRecognizer.delegate = nil;     } }  - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {     return NO; } 
like image 44
Antoine Avatar answered Sep 21 '22 09:09

Antoine