Danoli3
Danoli3

Reputation: 3233

How to turn off the automatic gesture to go back a view with a navigation controller?

So I'm noticing all of my views are receiving the gesture to go back (pop a view) when the user swipes on the very left side of the screen (in either orientation) (This is new with iOS7)

I've tried so far with no avail to turn it off using:

    [self.navigationItem setHidesBackButton:YES];

Within the init of the NavigationController itself (as the delegate seems to be using that).

Upvotes: 22

Views: 29704

Answers (6)

Gabriele Petronella
Gabriele Petronella

Reputation: 108101

obj-c

self.navigationController.interactivePopGestureRecognizer.enabled = NO;

swift

navigationController?.interactivePopGestureRecognizer?.isEnabled = false

Upvotes: 72

ilan
ilan

Reputation: 4462

For IOS 8 (Swift):

class MainNavigationController: UINavigationController {

    override func viewDidLoad() {
        super.viewDidLoad()
        self.interactivePopGestureRecognizer.enabled = false

        // Do any additional setup after loading the view.
    }

}

Upvotes: 3

Userich
Userich

Reputation: 316

I use this solution in my project, it disables only interactivePopGestureRecognizer and allows you to use another gesture recognizers.

- (void)viewDidLoad {

    [super viewDidLoad];

    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {

        self.navigationController.interactivePopGestureRecognizer.enabled = NO;
        self.navigationController.interactivePopGestureRecognizer.delegate = self;

    }

}


- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer {

    if ([gestureRecognizer isEqual:self.navigationController.interactivePopGestureRecognizer]) {

        return NO;

    } else {

        return YES;

    }

}

Upvotes: 5

Antoine
Antoine

Reputation: 23976

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;
}

Upvotes: 4

Shardul
Shardul

Reputation: 4274

Use this code for previous than iOS 7

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

Upvotes: 0

Danoli3
Danoli3

Reputation: 3233

Adding to Gabriele's Solution.

To support any iOS before iOS 7 you will need to wrap this code with this:

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

This will stop the App crashing in iOS 6 and iOS 5 for missing selector.

Upvotes: 6

Related Questions