[Solved] Navigation Bar gets funky if Swipe Gesture and Back Button are triggered at the same time


To fix this I disable user interactions for the navigationsbar. To do so, I subclass UINavigationViewController and use Key-Value-Observing to detect the state of the gesture recognizer.

#import "NavigationViewController.h"


@interface NavigationViewController ()
@end

@implementation NavigationViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.interactivePopGestureRecognizer addObserver:self
                                           forKeyPath:@"state"
                                              options:(NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld)
                                              context:NULL];
}




- (void)observeValueForKeyPath:(NSString *)keyPath
                      ofObject:(id)object
                        change:(NSDictionary *)change
                       context:(void *)context
{
    if ([keyPath isEqual:@"state"]) {
        [self recognizer:object
            changedState:[change[@"new"] integerValue]
                oldState:[change[@"old"] integerValue]];

    } else {
        [super observeValueForKeyPath:keyPath
                             ofObject:object
                               change:change
                              context:context];
    }
}

- (void)recognizer:(UIGestureRecognizer *)recognizer
      changedState:(UIGestureRecognizerState)newState
          oldState:(UIGestureRecognizerState)oldState
{
    switch (newState) {
        case UIGestureRecognizerStateEnded:
        case UIGestureRecognizerStateCancelled:
        case UIGestureRecognizerStateFailed:
            [self.navigationBar setUserInteractionEnabled:YES]; break;

        case UIGestureRecognizerStateBegan:
            [self.navigationBar setUserInteractionEnabled:NO]; break;

        default:
            break;
    }
}


- (void)dealloc
{
    [self.interactivePopGestureRecognizer removeObserver:self forKeyPath:@"state"];
}

@end

You will find the fixed code on GitHub as-well.

0

solved Navigation Bar gets funky if Swipe Gesture and Back Button are triggered at the same time