I found a solution:
Objective-C:
if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
self.navigationController.interactivePopGestureRecognizer.enabled = NO;
}
Swift 3+:
self.navigationController?.interactivePopGestureRecognizer?.isEnabled = false
Not sure if you already resolved this but I'm facing the same issue but with one difference. The navigation stack only messes up if I setAnimated to NO.
So this works:
[self.navigationController setNavigationBarHidden:YES animated:YES];
...
[self.navigationController setNavigationBarHidden:NO animated:YES];
but this doesn't:
[self.navigationController setNavigationBarHidden:YES animated:NO];
...
[self.navigationController setNavigationBarHidden:NO animated:NO];
If you really want animated to be NO for whatever reason, one work around is to set alpha to 0/1 instead of hiding/unhiding the NavigationBar:
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.navigationController.navigationBar.alpha = 0.0f;
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[self.navigationController.navigationBar setAlpha:1.0f];
}
The downside is that there's no nice slide-to-pop transition animation. If you did find a better way, do let us know.
UPDATE: This is now old but I solved my issue by not ensuring that whatever state is changed in current view's viewWillDisappear, is restored in viewWillAppear. Don't tear down things in viewWillDisappear that you can't setup again.
This is what happens when you cancel the pop animation:
- Current viewWillDisappear
- New viewWillAppear
- [cancelled... reverses]
- New viewWillDisappear
- New viewDidDisappear
- Current viewWillAppear
- Current viewDidAppear
I guess in this brave new world, viewWillDisappear/viewWillAppear does not always mean view "will" disappear/appear :)
Best Answer
I have found that when using custom back buttons, the interactive pop gesture stops working (my take is that Apple cannot foresee how your custom back button will behave, so they disable the gesture).
To fix this, as other mentioned before, you can set the
interactivePopGestureRecognizer.delegate
property tonil
.In Swift,
this can easily be done across your entire application by adding an extension for:UINavigationController
like thisUpdated answer
Seems like setting the delegate to
nil
causes the app UI to freeze in some scenarios (eg. when the user swipes left or right on the top view controller of the navigation stack).Because
gestureRecognizerShouldBegin
delegate method cannot be handled in an extension, subclassingUINavigationController
seems like the best solution: