ryutamaki
ryutamaki

Reputation: 109

self.navigationItem.hidesBackButton = YES and go back with swipe gesture on iOS7

I write the codes below in my UIViewController that uses UINavigationController.

- (void)viewDidLoad {
    [super viewDidLoad];

    self.navigationItem.hidesBackButton = YES;
    self.navigationController.interactivePopGestureRecognizer.delegate = (id<UIGestureRecognizerDelegate>) self;
}

I build and run my app,

self.navigationItem.hidesBackButton = YES;

above that works correctly, but

self.navigationController.interactivePopGestureRecognizer.delegate = (id<UIGestureRecognizerDelegate>) self;

that one DO NOT works.

So, I re-write the code below.

- (void)viewDidLoad {
    UIBarButtonItem *backBarButton = [[UIBarButtonItem alloc] initWithCustomView:[[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 44.0f, 44.0f)]];
    backBarButton.tintColor = [UIColor clearColor];
    self.navigationController.interactivePopGestureRecognizer.delegate = (id<UIGestureRecognizerDelegate>)self;
    self.navigationItem.leftBarButtonItem = backBarButton;
}

It works correctly.

However, I want to use the first example. The first one clearly express what I want to do.

Does someone have any idea?

Upvotes: 0

Views: 1801

Answers (1)

Austin
Austin

Reputation: 5655

In viewDidLoad, the view controller is not yet contained in a navigation controller, so the navigationController property is nil, which is why that line has no effect.

That said, assigning the delegate of UINavigationController's interactivePopGestureRecognizer is not good practice (I'm pretty sure it expects to be assigned to the navigation controller). Try disabling the gesture recognizer in viewWillAppear: instead:

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.interactivePopGestureRecognizer.enabled = NO;
}

Upvotes: 1

Related Questions