I am trying to disable the back gesture for my view controller using the following set of code.
In FirstViewController.m
, I\'m setting the delegate of <
I originally put these answers into a comment below the accepted answer, but I feel this needs to be said as an answer to get more visibility.
More often than not, you will find that the accepted answer does not work. This is because viewWillAppear:
can be called before the view is added to a navigation controller's view hierarchy, and so self.navigationController
is going to be nil
. Because of this, the interactivePopGestureRecognizer may not be disabled in some cases. You're better off calling it in viewDidAppear:
instead.
Here's code that will work (assuming your view controller is correctly added to a navigation controller's view hierarchy):
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[[[self navigationController] interactivePopGestureRecognizer] setEnabled:NO];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[[[self navigationController] interactivePopGestureRecognizer] setEnabled:YES];
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
navigationController?.interactivePopGestureRecognizer?.isEnabled = false
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
navigationController?.interactivePopGestureRecognizer?.isEnabled = true
}