lukech lukech - 1 month ago 15
iOS Question

Discern between UINavigation bar back button, and interactive pop swipe to go back?

I need to discern between the two methods of going back in iOS – tapping the back button in a navigation bar, and using the screen-edge pan to do so. I’d rather not implement a custom back button if possible.

How might I go about this one?

Answer

Figured it out:

I’ve implemented UINavigationControllerDelegate, declared myself as that delegate, and set up a boolean value called isPanningBack. Then implemented this guy;

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    id<UIViewControllerTransitionCoordinator> tc = navigationController.topViewController.transitionCoordinator;
    [tc notifyWhenInteractionEndsUsingBlock:^(id<UIViewControllerTransitionCoordinatorContext> context) {
        if (![context isCancelled]) {
            _isPanningBack = YES;
        }
    }];
}

Then, implemented viewDidDisappear:animated:

- (void)viewDidDisappear:(BOOL)animated
{
    if (!_isPanningBack) {
        // We can be sure that we used the chevron.
    } else {
        // We can be sure that we used the swipe.
    }
    [super viewDidDisappear:animated];
}