UINavigationContoller interactivePopGestureRecogni

2019-03-09 10:31发布

I have a view controller which is nested within a UINavigationController.

I have implemented the iOS 7 interactivePopGestureRecognizer to enable the user to gesture to pop a VC off the stack.

Within the VC i have a scrollview and whilst the user is not at the top of the scrollview I hide all the chrome (Navigation bar and status bar) to place focus on the content.

However with the navigation bar hidden, the interactivePopGestureRecognizer is not working.

I have tried enabling it after it has disappeared and verified it is not nil, however it still doesn't work.

Is there anything I am missing?

5条回答
Ridiculous、
2楼-- · 2019-03-09 11:04

Simple solution

Just set the hidden property of the navigation bar not through navigation controller

Just use these two lines

self.navigationController.navigationBarHidden = NO;
self.navigationController.navigationBar.hidden = YES;
查看更多
戒情不戒烟
3楼-- · 2019-03-09 11:15

This doesn't seem to work for me. I followed Keithl's blog post. Neither did that work.

I ultimately settled with UISwipeGestureRecognizer. It seems to do what it says.

UISwipeGestureRecognizer *gestureRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(backButtonPressed:)];
[gestureRecognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
[self.navigationController.view addGestureRecognizer:gestureRecognizer];
查看更多
神经病院院长
4楼-- · 2019-03-09 11:20

I used this. self.navigationController.interactivePopGestureRecognizer.delegate = self;

also in my UINavigationController class to disable interactivePopGestureRecognizer during transitions.

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

    [super pushViewController:viewController animated:animated];
}

- (void)navigationController:(UINavigationController *)navigationController
       didShowViewController:(UIViewController *)viewController
                    animated:(BOOL)animated
{
    if ([navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        // disable interactivePopGestureRecognizer in the rootViewController of navigationController
        if ([[navigationController.viewControllers firstObject] isEqual:viewController]) {
            navigationController.interactivePopGestureRecognizer.enabled = NO;
        } else {
            // enable interactivePopGestureRecognizer
            navigationController.interactivePopGestureRecognizer.enabled = YES;
        }
    }
}

the reason of disable interactivePopGestureRecognizer in rootViewController is:when swipe from edge in rootViewController and then tap something to push in next viewController, the UI won't accept any touches now.Press home button to put app in background , and then tap it to enter foreground...

查看更多
一夜七次
5楼-- · 2019-03-09 11:24

Set your UIViewController subclass as the gestureRecognizer's delegate:

self.navigationController.interactivePopGestureRecognizer.delegate = self;

That's it!

查看更多
Ridiculous、
6楼-- · 2019-03-09 11:24

Adding these two lines to -(void)viewDidAppear:(BOOL)animated worked for me.

self.navigationController.navigationBarHidden = NO;
self.navigationController.navigationBar.hidden = YES;

And dont forget to call <UIGestureRecognizerDelegate> to .h file.

查看更多
登录 后发表回答