Search code examples
iosobjective-cuinavigationcontrolleruinavigationbaruinavigationitem

How to enable back/left swipe gesture in UINavigationController after setting leftBarButtonItem?


I got the opposite issue from here. By default in iOS7, back swipe gesture of UINavigationController's stack could pop the presented ViewController. Now I just uniformed all the self.navigationItem.leftBarButtonItem style for all the ViewControllers.

Here is the code:

self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:LOADIMAGE(@"back_button") style:UIBarButtonItemStylePlain target:self action:@selector(popCurrentViewController)];

after that, the navigationController.interactivePopGestureRecognizer is disabled. How could I make the pop gesture enabled without removing the custom leftBarButtonItem?

Thanks!


Solution

  • First set delegate in viewDidLoad:

    self.navigationController.interactivePopGestureRecognizer.delegate = self;
    

    And then disable gesture when pushing:

    - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
        [super pushViewController:viewController animated:animated];
        self.interactivePopGestureRecognizer.enabled = NO;
    }
    

    And enable in viewDidDisappear:

    self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    

    Also, add UINavigationControllerDelegate to your view controller.