简体   繁体   中英

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?

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM