In iOS 7 there's the new swipe to pop gesture: You swipe from left to right on the left side of your screen and the UINavigationController pops back to the previous UIViewController.
When I create a custom back button like this, the swipe to pop gestures doesn't work anymore:
UIBarButtonItem *customBackButton = [[UIBarButtonItem alloc] initWithTitle:@" " style:UIBarButtonItemStyleBordered target:self action:@selector(navigateBack)];
[customBackButton setBackButtonBackgroundImage:barBackBtnImg forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[customBackButton setBackButtonBackgroundImage:barBackBtnImgHighlighted forBarMetrics:UIBarMetricsDefault];
self.navigationItem.backBarButtonItem = customBackButton;
How can I use a custom back button and have the native swipe to pop gesture?
Update:
That's what's happening in navigateBack:
- (void)navigateBack {
[self.navigationController popViewControllerAnimated:YES];
}
There is no need to add your own gesture recognizer. The UINavigationController already does that for you. You need to set the delegate for the interactivePopGestureRecognizer before enabling it.
Do the following two things:
self.navigationController.interactivePopGestureRecognizer.delegate = (id<UIGestureRecognizerDelegate>)self;
[self.navigationController.interactivePopGestureRecognizer setEnabled:YES];
Just add the following line of code:
[self.navigationController.interactivePopGestureRecognizer addTarget:self action:@selector(handleGesture:)];
You can add your own UIGestureRecognizer and pop the UIViewController yourself. See the docs for further info.
I use
[[UINavigationBar appearance] setBackIndicatorImage:[UIImage imageNamed:@"nav_back.png"]];
[[UINavigationBar appearance] setBackIndicatorTransitionMaskImage:[UIImage imageNamed:@"nav_back.png"]];
[UIBarButtonItem.appearance setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -64) forBarMetrics:UIBarMetricsDefault];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With