8

Here is a ViewControllerA push a ViewControllerB, and in the ViewControllerB the leftBarButtonItem is set as following:

self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:self action:@selector(backBtnClicked:)];

After setting the leftBarButtonItem, the Back swipe gesture is not work. Is it possible to keep the swipe gesture?

lu yuan
  • 7,207
  • 9
  • 44
  • 78

2 Answers2

25

Because you've changed the left bar button item, you're telling the navigation controller to stop managing the navigation-based back-actions that the user can take.

To fix it, you can tell the navigation controller to continue accepting those gestures on the current view controller by using:

self.navigationController.interactivePopGestureRecognizer.delegate = self;

Where self if your view controller.

UIViewController privately implements UIGestureRecognizerDelegate, so you'll get a warning for this, but you can mitigate this by adding in the protocol conformance (<UIGestureRecognizerDelegate>) to your header, or to a class extension.

Eagerod
  • 811
  • 9
  • 11
  • I've heard theres issues with this, but it appears to work at first glance. anyone run into weird issues using this? – skinsfan00atg Apr 03 '14 at 16:31
  • 3
    I found one reference to potential issues: Specifically the issue would be, if you try to swipe while another pop animation is already happening, it will cause the exception where you can't do multiple transitions at once. The proposed solution is to make a custom UINavigationController where the navigation controller itself is the interactionPopGestureRecognizer delegate, and it will ignore swipes while another navigation transition is in progress. I prefer UINavigationBar setBackIndicatorImage to set a custom image for the back button. – Matthew Horst Sep 02 '15 at 13:49
0

try to set interactivePopGestureRecognizer to nil

override func viewDidLoad() {
    super.viewDidLoad()
    self.interactivePopGestureRecognizer!.delegate = nil;

}
DàChún
  • 4,751
  • 1
  • 36
  • 39