简介
- 继承于:UIViewController:UIResponder:NSObject
遵守: UIAppearanceContainer, NSObject, NSCoding,
UIContentContainer, UITraitEnvironment(PS: You generally use this class as-is but you may also subclass to customize the class behaviour)
用栈(navigation stack)即一个VC数组来管理屏幕上的显示,该 VC数组第一个便是根视图控制器(root view controller), 最后一个就是正在显示的VC,可通过segues或该类的某些方法向该栈添加或移除VC,可通过返回按钮或左边缘轻扫手势移除顶层VC;
顶部的navigation bar 和底部的optional toolbar的显示和控制都由navigation controller自己控制。另外,对于试图控制器的pushing或popping,动画等可重写 UINavigationControllerDelegate 协议
Navigation controller views
一个导航控制器所含视图= window +Tab bar view + navigation view + custom view hierarchy
(content view 会覆盖navigation bar ,所以注意布局)
不能直接改变工具条的frame,bounds,alpha值,但可继承
UINavigationBar, 必须重写初始化方法:
initWithNavigationBarClass:toolBarClass:
(可直接用navigationBarHidden 属性或
setNavigationBarHidden:animated:方法来隐藏或显示导航条)
导航控制器用navigation item对象动态显示导航条的内容(navigation item objects (instances of the UINavigationItem class) associated with the view controllers on the navigation stack)可用UIAppearance APIs 来自定义导航栏的总体外观,以及配置当前VC的navigation items 来改变导航栏的内容。
Updating the Navigation Bar
导航条上有左中右三个bar button( UIBarButtonItem 类)可根据需要添加。
导航条的属性:1>tintColor控制导航条items的颜色
2>barTintColor控制导航条本身的颜色
leftBarButtonItem titleView rightBarButtonItem属性
Displaying a Toolbar
默认隐藏,可通过导航控制器实例中调用setToolbarHidden:animated:
方法让其显示。自定义:继承UIToolbar类
具体用法
- (instancetype)initWithRootViewController:(UIViewController *)rootViewController
rootViewController不能是UITabBarController类的实例
Accessing Items on the Navigation Stack
属性:
topViewController (read-only)
visibleViewController(read-only)
viewControllers(在导航栈里的VC,NSArray类型)
方法:
setViewControllers:animated:
更新或替代当前的VC栈,一般在程序启动时(launch time)调用返回一个导航控制器
- (void)setViewControllers:(NSArray<UIViewController *> *)viewControllers animated:(BOOL)animated
Pushing and Popping Stack Items
属性:
interactivePopGestureRecognizer
用这个属性取到导航控制器内置的手势识别器,用它绑定指定方法来设定pop顶层VC样式
@property(nonatomic, readonly) UIGestureRecognizer *interactivePopGestureRecognizer
方法:
pushViewController:animated:
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
popViewControllerAnimated:
移除栈中顶层VC,并返回,然后更新display
- (UIViewController *)popViewControllerAnimated:(BOOL)animated
popToRootViewControllerAnimated:
除了根视图控制器,移除所有
- (NSArray<__kindofUIViewController *> *)popToRootViewControllerAnimated:(BOOL)animated
popToViewController:animated:
pop 所有VC直到指定的VC在最上层
- (NSArray<__kindofUIViewController *> *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated
Configuring Navigation Bars & Custom Toolbars
属性:
//属于导航控制器的
@property(nonatomic, readonly) UINavigationBar *navigationBar
@property(nonatomic, readonly) UIToolbar *toolbar
@property(nonatomic, getter=isNavigationBarHidden) BOOL navigationBarHidden
@property(nonatomic, getter=isToolbarHidden) BOOL toolbarHidden
//利用手势隐藏导航条和导航栏(iOS 8.0)
@property(nonatomic, readwrite, assign) BOOL hidesBarsOnTap
@property(nonatomic, readwrite, assign) BOOL hidesBarsOnSwipe
@property(nonatomic, readonly, assign) UITapGestureRecognizer *barHideOnTapGestureRecognizer
@property(nonatomic, readonly, strong) UIPanGestureRecognizer *barHideOnSwipeGestureRecognizer
//特殊情况下隐藏
@property(nonatomic, readwrite, assign) BOOL hidesBarsWhenVerticallyCompact
@property(nonatomic, readwrite, assign) BOOL hidesBarsWhenKeyboardAppears
方法:
- (void)setNavigationBarHidden:(BOOL)hidden
animated:(BOOL)animated
- (void)setToolbarHidden:(BOOL)hidden
animated:(BOOL)animated