How to Enable Back/Left Swipe Gesture in Uinavigationcontroller After Setting Leftbarbuttonitem

How to enable back/left swipe gesture in UINavigationController after setting leftBarButtonItem?

First set delegate in viewDidLoad:

self.navigationController.interactivePopGestureRecognizer.delegate = self;

And then disable gesture when pushing:

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
[super pushViewController:viewController animated:animated];
self.interactivePopGestureRecognizer.enabled = NO;
}

And enable in viewDidDisappear:

self.navigationController.interactivePopGestureRecognizer.enabled = YES;

Also, add UINavigationControllerDelegate to your view controller.

How to disable back swipe gesture in UINavigationController on iOS 7

I found a solution:

Objective-C:

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

Swift 3+:

self.navigationController?.interactivePopGestureRecognizer?.isEnabled = false

Replace back button but keeping swipe to navigate back

I used this and it worked:

self.navigationController.interactivePopGestureRecognizer.delegate = nil;

Back swipe gesture is not work when I add the leftBarButtonItem

Because you've changed the left bar button item, you're telling the navigation controller to stop managing the navigation-based back-actions that the user can take.

To fix it, you can tell the navigation controller to continue accepting those gestures on the current view controller by using:

self.navigationController.interactivePopGestureRecognizer.delegate = self;

Where self if your view controller.

UIViewController privately implements UIGestureRecognizerDelegate, so you'll get a warning for this, but you can mitigate this by adding in the protocol conformance (<UIGestureRecognizerDelegate>) to your header, or to a class extension.

How to disable back/left swipe gesture?

From the controller you want this to be enabled/disabled just

Swift:

self.navigationController?.interactivePopGestureRecognizer?.isEnabled = false // or true

ObjC:

self.navigationController.interactivePopGestureRecognizer.enabled = NO; // or YES

Overriding back swipe gesture in UINavigationController

You can do this in a combination of the following:

Add a swipe gesture recognizer to your view controller:

Sample Image

Add the following to your view controller class:

import UIKit

class SwipeBackViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()

navigationController?.interactivePopGestureRecognizer?.isEnabled = false
}

@IBAction func swipeback(_ sender: UISwipeGestureRecognizer) {
navigationController?.popToRootViewController(animated: true)
}
}
  • The command in viewDidLoad disables the default swipe recogninzer in iOS
  • Then, the action associated with the swipe recognizer you added above handles the pop for you

My answer here goes into disabling the recognizer in more detail in case you have any questions on that.

Changing back button in iOS 7 disables swipe to navigate back

IMPORTANT:
This is a hack. I would recommend taking a look at this answer.

Calling the following line after assigning the leftBarButtonItem worked for me:

self.navigationController.interactivePopGestureRecognizer.delegate = self;

Edit:
This does not work if called in init methods. It should be called in viewDidLoad or similar methods.

Disable swipe back gesture in Swift

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;


Related Topics



Leave a reply



Submit