通过NavigationControllerDelegate在交互式转换期间隐藏/取消隐藏导航栏的正确方法是什么?

xap*_*hod 7 cocoa-touch objective-c ios

这与我的另一个问题有关,iOS 8 +交互式过渡+导航条显示=坏了吗?,但是不同.

在iOS 8上,当一个人通过NavigationControllerDelegate/ UIViewControllerInteractiveTransitioning方法进行从视图A到视图B的交互式转换,而视图A有一个导航栏,而视图B没有,那么隐藏/取消隐藏导航栏的正确方法是什么?

我尝试在ViewController中执行此操作,如下所示:

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    [[self transitionCoordinator] animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context) {

        if (self.navigationController) {
            [self.navigationController setNavigationBarHidden:YES animated:animated];
        }

    } completion:^(id<UIViewControllerTransitionCoordinatorContext> context) {

        NSArray *debugViews = [context containerView].subviews;
        NSLog(@"%@", debugViews);

        if ([context isCancelled] ) {
            if( self.navigationController ) {
                [self.navigationController setNavigationBarHidden:NO animated:animated];
            }
        }
    }];
}

- (void)viewWillDisappear:(BOOL)animated {

    [[self transitionCoordinator] animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context) {

        if (self.navigationController) {
            [self.navigationController setNavigationBarHidden:NO animated:animated];
        }

    } completion:^(id<UIViewControllerTransitionCoordinatorContext> context) {

        if ([context isCancelled] ) {
            if( self.navigationController ) {
                [self.navigationController setNavigationBarHidden:YES animated:animated];
            }
        }
    }];

    [super viewWillDisappear:animated];
}
Run Code Online (Sandbox Code Playgroud)

......但是有两个大问题:

  1. 视图(主要是导航栏)有时在动画完成时"闪烁".如果你有一个复杂的视图,这真的很难看.

  2. 如果用户取消了交互式转换(即通过不拖动足够的距离或捏足够),则导航栏将永远消失,即使我可以在代码中看到它被告知取消隐藏.

以下是一些显示这种情况的源代码:https://github.com/xaphod/UIViewControllerTransitionTut

- > un-pinch从一个视图控制器转到另一个视图控制器; 第一个视图有一个导航栏,第二个视图没有.完成转换后,您有时会看到闪烁(上面的问题1).当你松开一点然后放开时,这是一个被取消的过渡:虽然你仍然在视图1上,但导航栏已经消失(上面的问题2).

Viz*_*llx 1

隐藏导航栏的正确方法是使用导航的控制器委托,请确保在使用以下委托方法之前将窗口的导航控制器委托设置为 self:-

只需在 AppDelegate.m 中执行此操作

- (BOOL)application:(UIApplication *)application 
        didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
 {
        self.window.rootViewController.navigationController.delegate=self;

    //do your rest code...


  }

-(void)navigationController:(UINavigationController *)navController 
             willShowViewController:(UIViewController *)viewController 
                           animated:(BOOL)animated 
{
            [navController setNavigationBarHidden:([viewController isKindOfClass:[CustomViewController class]]) 
                              animated:animated];   // just mention the view controller class type for which  you want to hide 
}
Run Code Online (Sandbox Code Playgroud)

从此SFO转介