iOS开发UINavigation系列四——导航控制器UINavigationController
iOS開(kāi)發(fā)UINavigation系列四——導(dǎo)航控制器UINavigationController
一、引言
????????在前面的博客中,我么你介紹了UINavigationBar,UINavigationItem和UIToolBar,UINavigationController是將這些控件和UIViewController緊密的結(jié)合了起來(lái),使用導(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,通過(guò)push與pop進(jìn)行controller的切換,我們有兩種方式可以創(chuàng)建導(dǎo)航控制器:
?| 1 2 3 4 | //通過(guò)一個(gè)自定義的導(dǎo)航欄和工具欄創(chuàng)建導(dǎo)航控制器 -?(instancetype)initWithNavigationBarClass:(nullable?Class)navigationBarClass?toolbarClass:(nullable?Class)toolbarClass; //使用系統(tǒng)默認(rèn)的導(dǎo)航欄和工具欄,通過(guò)一個(gè)根視圖創(chuàng)建導(dǎo)航控制器 -?(instancetype)initWithRootViewController:(UIViewController?*)rootViewController; |
通過(guò)以下方法對(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; //彈出鍵盤(pán)的時(shí)候隱藏導(dǎo)航欄 @property?(nonatomic,?readwrite,?assign)?BOOL?hidesBarsWhenKeyboardAppears; //屏幕滑動(dòng)的時(shí)候隱藏導(dǎo)航欄,常用于tableView,上滑隱藏導(dǎo)航欄,下滑顯示,帶動(dòng)畫(huà)效果 @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中增加的這些方法,不得不說(shuō)著實(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)畫(huà)進(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?*navigationController; |
? ??
總結(jié)
以上是生活随笔為你收集整理的iOS开发UINavigation系列四——导航控制器UINavigationController的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: WDS使用捕获映像制作企业自定义映像
- 下一篇: mysql-data-dumper