Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS 7 Back Button Pop Gesture

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];
}
like image 230
dlinsin Avatar asked Sep 18 '13 15:09

dlinsin


3 Answers

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];
like image 91
udit gupta Avatar answered Nov 02 '22 03:11

udit gupta


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.

like image 42
prizzl Avatar answered Nov 02 '22 03:11

prizzl


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];
like image 3
Albert Chu Avatar answered Nov 02 '22 03:11

Albert Chu