How to Disable Back Swipe Gesture in Uinavigationcontroller on iOS 7

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

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

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;

How do disable back swipe gesture in UINavigationController in iOS 8?

Setting a custom back button image generally does the trick.

self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc]
initWithTitle:@""
style:UIBarButtonItemStylePlain
target:nil
action:nil];

As an aside I generally recommend against breaking the built in behaviors like this, especially since most iOS users are very familiar and comfortable with these gestures. Taking them out just serves to frustrate users unnecessarily.

So if you are using the system standard navigation bars then it definitely stands to reason that people expect system standard behaviors. If you don't want these behaviors then you are better off customizing the UI: In this scenario this means either using your own Navigation bar and setting the navigation controller's bar to hidden.

How to disable swipe back gesture with master-detail view in ios 11?

I figured it out. I had to add this to my DetailViewController

id savedGestureRecognizerDelegate1;
id savedGestureRecognizerDelegate2;

- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
savedGestureRecognizerDelegate1 = self.navigationController.interactivePopGestureRecognizer.delegate;
self.navigationController.interactivePopGestureRecognizer.delegate = self;
}
if ([self.navigationController.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
savedGestureRecognizerDelegate2 = self.navigationController.navigationController.interactivePopGestureRecognizer.delegate;
self.navigationController.navigationController.interactivePopGestureRecognizer.delegate = self;
}
}

- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
self.navigationController.interactivePopGestureRecognizer.delegate = savedGestureRecognizerDelegate1;
}
if ([self.navigationController.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
self.navigationController.navigationController.interactivePopGestureRecognizer.delegate = savedGestureRecognizerDelegate2;
}
}

- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
if (gestureRecognizer == self.navigationController.interactivePopGestureRecognizer || gestureRecognizer == self.navigationController.navigationController.interactivePopGestureRecognizer) {
return NO;
}
return YES;
}

This is because there's two navigation controllers in a split view controller. The saved gesture recognizer delegate is because of this.

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.

How to disable swipe back specifically on only one view controller

A better approach is to clear them from the stack when you show UserProfileVC

let profile  = self.storyboard?.instantiateViewController(withIdentifier: "profileID") as! UserProfileVC
self.navigationController?.viewControllers = [profile]

Edit: Do this inside profileVC

self.navigationController?.viewControllers = [self]

//

self.view.alpha = 0

UIView.animate(withDuration: 0.5) {

self.view.alpha = 1

}

iOS 7 disable the swipe back

You can disable it through the public API, See UINavigationController Class Reference

  //iOS7 Customization, swipe to pop gesture
if ([navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
navigationController.interactivePopGestureRecognizer.enabled = NO;
}

Also, you can switch back to previous state when needed



Related Topics



Leave a reply



Submit