Navigation stack becomes unusable after canceling iOS 7 back swipe gesture

吃可爱长大的小学妹 提交于 2019-11-28 18:40:50

Not sure if you already resolved this but I'm facing the same issue but with one difference. The navigation stack only messes up if I setAnimated to NO.

So this works:

[self.navigationController setNavigationBarHidden:YES animated:YES];
...
[self.navigationController setNavigationBarHidden:NO animated:YES];

but this doesn't:

[self.navigationController setNavigationBarHidden:YES animated:NO];
...
[self.navigationController setNavigationBarHidden:NO animated:NO];

If you really want animated to be NO for whatever reason, one work around is to set alpha to 0/1 instead of hiding/unhiding the NavigationBar:

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.alpha = 0.0f;
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.navigationController.navigationBar setAlpha:1.0f];
}

The downside is that there's no nice slide-to-pop transition animation. If you did find a better way, do let us know.

UPDATE: This is now old but I solved my issue by not ensuring that whatever state is changed in current view's viewWillDisappear, is restored in viewWillAppear. Don't tear down things in viewWillDisappear that you can't setup again.

This is what happens when you cancel the pop animation:


  1. Current viewWillDisappear
  2. New viewWillAppear
  3. [cancelled... reverses]
  4. New viewWillDisappear
  5. New viewDidDisappear
  6. Current viewWillAppear
  7. Current viewDidAppear

I guess in this brave new world, viewWillDisappear/viewWillAppear does not always mean view "will" disappear/appear :)

I think my solution may help you.

Let's assume that the class of your homepage view controller is HomePageViewController(class name), and the activity view controller is called ActivityViewController(class name, too)

First, in your HomePageViewController's viewWillAppear:

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES];
}

Secondly, in your ActivityViewController, add these codes:

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}

- (void)viewWillDisappear:(BOOL)animated
{
    //trick to fix navigationbar disappear problem that when UIScreenEdgePanGesture is happening.
    if ([self.navigationController.topViewController isKindOfClass:[HomePageViewController class]]) {
        [self.navigationController setNavigationBarHidden:YES animated:YES];
    } else {
        [self.navigationController setNavigationBarHidden:NO animated:YES];
    }
    [super viewWillDisappear:animated];
}

I had the same problem, and after searching around, it looks like you can't get a notification if the gesture is cancelled. There is no -shouldCancel on the gesture recognizer delegate, and there is no -willReshowViewController on the navigation controller. So I have the view controller that needs the navigation bar shown (i.e. the controller that is being cancelled on) set self.navigationController.navigationBarHidden = NO in -viewWillAppear. That way when the gesture is cancelled and -viewWillAppear is called, the navigation bar will be unhidden again. As far as I can tell, this is the only way to have both the swipe-back gesture and shown/hidden navigation bars without a problem when cancelling the gesture.

Its will help you to avoid freezing the app.

- (void)navigationController:(UINavigationController *)navigationController
       didShowViewController:(UIViewController *)viewController
                    animated:(BOOL)animate
{
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)])
    {
        if (self.navigationController.viewControllers.count > 1)
        {
            self.navigationController.interactivePopGestureRecognizer.enabled = YES;
        }
        else
        {
            self.navigationController.interactivePopGestureRecognizer.enabled = NO;
        }
    }
}

I was able to resolve this issue by using animated: true in viewWillAppear and animated: false in viewWillDisappear of the 2nd view controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!