UINavigationController

UINavigationController : UIViewController

基础

    // 初始化,也可以 storyboard 创建(略)
    TESTVC *testVC = [[TESTVC alloc] init];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:testVC];
    
    // 转场
    [nav pushViewController:testVC2 animated:YES];
    [nav popViewControllerAnimated:YES];
    [nav popToRootViewControllerAnimated:YES];
    [nav popToViewController:testVC3 animated:YES];

常量

// 0.2 s
UIKIT_EXTERN const CGFloat UINavigationControllerHideShowBarDuration;

viewController

    // 栈顶层的
    UIViewController *topViewController = self.navigationController.topViewController;
    
    // 当前可见的(比如 present 的 VC,并不在栈内)
    UIViewController *visibleViewController = self.navigationController.visibleViewController;
    
    // 控制器栈
    NSArray *viewControllers = self.navigationController.viewControllers;
    
    // 设置控制器栈
    [self.navigationController setViewControllers:@[]];

navigationBar

    // navigationBar,详细可参考 UINavigationBar 深入研究
    UINavigationBar *navigationBar = self.navigationController.navigationBar;

    // 属性
    self.navigationController.navigationBarHidden = YES;
    
    // 方法,可选择动画
    [self.navigationController setNavigationBarHidden:YES animated:YES];

toolbar


    // toolbar,详细可参考 UIToolbar 深入研究,大致类似 UITabBar
    UIToolbar *toolbar = self.navigationController.toolbar;

    // 属性
    self.navigationController.toolbarHidden = YES;
    
    // 方法,可选择动画
    [self.navigationController setToolbarHidden:NO animated:YES];

iOS 7 侧边返回手势

    // 可深入研究 UIGestureRecognizer 部分
    UIGestureRecognizer *popGesture = self.navigationController.interactivePopGestureRecognizer;

iOS 8

  • 属性
    // 键盘出现隐藏 Bar
    self.hidesBarsWhenKeyboardAppears = YES;
    
    // scrollerView 上滑隐藏,下滑出现
    self.hidesBarsOnSwipe = YES;
    
    // 垂直方向不足时隐藏,比如横屏
    self.hidesBarsWhenVerticallyCompact = YES;
    
    // 点击隐藏、显示切换
    self.hidesBarsOnTap = YES;
    
    // 滑动手势
    UIPanGestureRecognizer *pan = self.barHideOnSwipeGestureRecognizer;
    // 点击手势
    UITapGestureRecognizer *tap = self.barHideOnTapGestureRecognizer;
  • 方法
    // showViewController 作为多功能函数,不同的 VC 可以有不同的效果,nav 使用,则就是 push,可重写自定义
    [self.navigationController showViewController:vc sender:@"123"];

UINavigationControllerDelegate

  • 生命周期
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    NSLog(@"willShowViewController");
}

- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    NSLog(@"didShowViewController");
}
  • 其他(屏幕旋转与转场方式,内容与 TabBarController 一致)

- (UIInterfaceOrientationMask)navigationControllerSupportedInterfaceOrientations:(UINavigationController *)navigationController {
    return UIInterfaceOrientationMaskLandscapeLeft;
}

- (UIInterfaceOrientation)navigationControllerPreferredInterfaceOrientationForPresentation:(UINavigationController *)navigationController {
    return UIInterfaceOrientationLandscapeRight;
}

- (nullable id <UIViewControllerInteractiveTransitioning>)navigationController:(UINavigationController *)navigationController
                                   interactionControllerForAnimationController:(id <UIViewControllerAnimatedTransitioning>) animationController {
    return [[TestInteractiveTransition alloc] init];
}

- (nullable id <UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController
                                            animationControllerForOperation:(UINavigationControllerOperation)operation
                                                         fromViewController:(UIViewController *)fromVC
                                                           toViewController:(UIViewController *)toVC {
    return [[TestTransition alloc] init];
}

拓展

大致用于快速获取 Nav 上的对象

UIViewController (UINavigationControllerItem)

    self.navigationController;
    self.navigationItem;
    self.hidesBottomBarWhenPushed = YES;

UIViewController (UINavigationControllerContextualToolbarItems)

    self.toolbarItems;
    [self setToolbarItems:@[] animated:YES];