iOS開發(fā)UINavigation系列四——導(dǎo)航控制器UINavigationController
一局扶、引言
二约急、導(dǎo)航控制器的創(chuàng)建和controller的管理
三、導(dǎo)航控制器中的常用方法和屬性
四驳庭、iOS8后導(dǎo)航的新特性
五肘交、UINavigationDelegate
六、與UIViewController相關(guān)
iOS開發(fā)UINavigation系列四——導(dǎo)航控制器UINavigationController
一菇肃、引言
在前面的博客中地粪,我么你介紹了UINavigationBar,UINavigationItem和UIToolBar琐谤,UINavigationController是將這些控件和UIViewController緊密的結(jié)合了起來蟆技,使用導(dǎo)航,我們的應(yīng)用程序?qū)哟螘?huì)更加分明斗忌,對(duì)controller的管理也更加方便质礼。前幾篇博客地址如下:
UINavigationBar:http://my.oschina.net/u/2340880/blog/527706
UINavigationItem:http://my.oschina.net/u/2340880/blog/527781
UIToolBar:http://my.oschina.net/u/2340880/blog/528168
二、導(dǎo)航控制器的創(chuàng)建和controller的管理
導(dǎo)航控制器是一個(gè)堆棧結(jié)構(gòu)织阳,只是其中管理的對(duì)象是controller眶蕉,通過push與pop進(jìn)行controller的切換,我們有兩種方式可以創(chuàng)建導(dǎo)航控制器:
?
1
2
3
4
//通過一個(gè)自定義的導(dǎo)航欄和工具欄創(chuàng)建導(dǎo)航控制器
- (instancetype)initWithNavigationBarClass:(nullable Class)navigationBarClass toolbarClass:(nullable Class)toolbarClass;
//使用系統(tǒng)默認(rèn)的導(dǎo)航欄和工具欄唧躲,通過一個(gè)根視圖創(chuàng)建導(dǎo)航控制器 - (instancetype)initWithRootViewController:(UIViewController *)rootViewController;
通過以下方法對(duì)視圖控制器進(jìn)行管理操作:
?
1
2
3
4
5
6
7
8
9
10
//設(shè)置管理的視圖控制器
- (void)setViewControllers:(NSArray<UIViewController *> *)viewControllers animated:(BOOL)animated;
//壓入新的視圖控制器 - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated;
//彈出一個(gè)視圖控制器 返回的是pop的controller - (nullable UIViewController *)popViewControllerAnimated:(BOOL)animated;
//彈出到某個(gè)視圖控制器 返回所有pop的controller - (nullable NSArray<__kindof UIViewController *> *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated;
//直接pop到根視圖控制器造挽,返回所有被pop的controller - (nullable NSArray<__kindof UIViewController *> *)popToRootViewControllerAnimated:(BOOL)animated;
三、導(dǎo)航控制器中的常用方法和屬性
?
1
2
3
4
//返回棧頂?shù)腸ontroller
@property(nullable, nonatomic,readonly,strong) UIViewController *topViewController;
//返回顯示的controller
@property(nullable, nonatomic,readonly,strong) UIViewController *visibleViewController;
上面兩個(gè)方法的區(qū)別在于弄痹,topViewController是返回被push出的最后一個(gè)controller饭入,但是如果之后又有present進(jìn)行莫泰跳轉(zhuǎn),visibleViewController會(huì)返回當(dāng)前顯示的controller肛真。例如A-push-B-present-C谐丢,則topViewController會(huì)返回B,visibleViewController會(huì)返回C蚓让。
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//返回堆棧中所有的controller
@property(nonatomic,copy) NSArray<__kindof UIViewController *> *viewControllers;
//設(shè)置隱藏導(dǎo)航欄
@property(nonatomic,getter=isNavigationBarHidden) BOOL navigationBarHidden;
- (void)setNavigationBarHidden:(BOOL)hidden animated:(BOOL)animated;
//導(dǎo)航欄對(duì)象乾忱,只讀屬性
@property(nonatomic,readonly) UINavigationBar *navigationBar;
//隱藏狀態(tài)欄
@property(nonatomic,getter=isToolbarHidden) BOOL toolbarHidden NS_AVAILABLE_IOS(3_0); - (void)setToolbarHidden:(BOOL)hidden animated:(BOOL)animated;
//狀態(tài)欄對(duì)象
@property(null_resettable,nonatomic,readonly) UIToolbar *toolbar;
//導(dǎo)航中的返回手勢(shì)對(duì)象
//iOS7之后,在導(dǎo)航中右劃會(huì)進(jìn)行pop操作历极,設(shè)置這個(gè)的enable可以控制設(shè)置手勢(shì)是否失效
@property(nullable, nonatomic, readonly) UIGestureRecognizer *interactivePopGestureRecognizer;
四窄瘟、iOS8后導(dǎo)航的新特性
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
//這個(gè)方法是為了iOS方法的命名統(tǒng)一,在導(dǎo)航中趟卸,其作用和push一樣
- (void)showViewController:(UIViewController *)vc sender:(nullable id)sender;
//彈出鍵盤的時(shí)候隱藏導(dǎo)航欄
@property (nonatomic, readwrite, assign) BOOL hidesBarsWhenKeyboardAppears;
//屏幕滑動(dòng)的時(shí)候隱藏導(dǎo)航欄寞肖,常用于tableView,上滑隱藏導(dǎo)航欄纲酗,下滑顯示,帶動(dòng)畫效果
@property (nonatomic, readwrite, assign) BOOL hidesBarsOnSwipe;
//滑動(dòng)隱藏導(dǎo)航欄的手勢(shì)
@property (nonatomic, readonly, strong) UIPanGestureRecognizer *barHideOnSwipeGestureRecognizer;
//橫屏的時(shí)候隱藏導(dǎo)航欄
@property (nonatomic, readwrite, assign) BOOL hidesBarsWhenVerticallyCompact;
//敲擊屏幕可以隱藏與顯示導(dǎo)航欄
@property (nonatomic, readwrite, assign) BOOL hidesBarsOnTap;
//敲擊屏幕的手勢(shì)
@property (nonatomic, readonly, assign) UITapGestureRecognizer *barHideOnTapGestureRecognizer;
iOS8中增加的這些方法新蟆,不得不說著實(shí)在用戶體驗(yàn)生進(jìn)了一大步觅赊,從中也可以看出apple對(duì)于用戶體驗(yàn)度的用心。
五琼稻、UINavigationDelegate
導(dǎo)航控制器還提供了一些代理回調(diào)方法吮螺,如下:
?
1
2
3
4
5
6
7
8
9
10
11
//視圖將要展示時(shí)調(diào)用的方法
(void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated;
//視圖已經(jīng)展示時(shí)調(diào)用的方法(void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated;
//設(shè)置方法設(shè)置導(dǎo)航控制器支持的設(shè)備方向(UIInterfaceOrientationMask)navigationControllerSupportedInterfaceOrientations:(UINavigationController *)navigationController NS_AVAILABLE_IOS(7_0);
//這個(gè)方法設(shè)置導(dǎo)航控制器的首選設(shè)備方向(UIInterfaceOrientation)navigationControllerPreferredInterfaceOrientationForPresentation:(UINavigationController *)navigationController NS_AVAILABLE_IOS(7_0);
//下面兩個(gè)方法可以對(duì)導(dǎo)航的轉(zhuǎn)場(chǎng)動(dòng)畫進(jìn)行設(shè)置(nullable id <UIViewControllerInteractiveTransitioning>)navigationController:(UINavigationController *)navigationController interactionControllerForAnimationController:(id <UIViewControllerAnimatedTransitioning>) animationController;
-
(nullable id <UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC ;
六、與UIViewController相關(guān)當(dāng)一個(gè)controller被添加到導(dǎo)航中后帕翻,系統(tǒng)會(huì)為它分配一些屬性鸠补,如下:
?
1
2
3
4
5
6
//當(dāng)前controller對(duì)應(yīng)的導(dǎo)航項(xiàng)
@property(nonatomic,readonly,strong) UINavigationItem *navigationItem;
//push的時(shí)候隱藏底部欄,如push后隱藏tabbar
@property(nonatomic) BOOL hidesBottomBarWhenPushed;
//管理它的導(dǎo)航控制器
@property(nullable, nonatomic,readonly,strong) UINavigationController *navigationControll