iOS开发UINavigation系列四——导航控制器UINavigationController
iOS開發UINavigation系列四——導航控制器UINavigationController
一、引言
????????在前面的博客中,我么你介紹了UINavigationBar,UINavigationItem和UIToolBar,UINavigationController是將這些控件和UIViewController緊密的結合了起來,使用導航,我們的應用程序層次會更加分明,對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
二、導航控制器的創建和controller的管理
????????導航控制器是一個堆棧結構,只是其中管理的對象是controller,通過push與pop進行controller的切換,我們有兩種方式可以創建導航控制器:
?| 1 2 3 4 | //通過一個自定義的導航欄和工具欄創建導航控制器 -?(instancetype)initWithNavigationBarClass:(nullable?Class)navigationBarClass?toolbarClass:(nullable?Class)toolbarClass; //使用系統默認的導航欄和工具欄,通過一個根視圖創建導航控制器 -?(instancetype)initWithRootViewController:(UIViewController?*)rootViewController; |
通過以下方法對視圖控制器進行管理操作:
| 1 2 3 4 5 6 7 8 9 10 | //設置管理的視圖控制器 -?(void)setViewControllers:(NSArray<UIViewController?*>?*)viewControllers?animated:(BOOL)animated; //壓入新的視圖控制器 -?(void)pushViewController:(UIViewController?*)viewController?animated:(BOOL)animated; //彈出一個視圖控制器?返回的是pop的controller -?(nullable?UIViewController?*)popViewControllerAnimated:(BOOL)animated; //彈出到某個視圖控制器?返回所有pop的controller -?(nullable?NSArray<__kindof?UIViewController?*>?*)popToViewController:(UIViewController?*)viewController?animated:(BOOL)animated;? //直接pop到根視圖控制器,返回所有被pop的controller -?(nullable?NSArray<__kindof?UIViewController?*>?*)popToRootViewControllerAnimated:(BOOL)animated; |
三、導航控制器中的常用方法和屬性
? | 1 2 3 4 | //返回棧頂的controller @property(nullable,?nonatomic,readonly,strong)?UIViewController?*topViewController;? //返回顯示的controller @property(nullable,?nonatomic,readonly,strong)?UIViewController?*visibleViewController; |
上面兩個方法的區別在于,topViewController是返回被push出的最后一個controller,但是如果之后又有present進行莫泰跳轉,visibleViewController會返回當前顯示的controller。例如A-push-B-present-C,則topViewController會返回B,visibleViewController會返回C。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | //返回堆棧中所有的controller @property(nonatomic,copy)?NSArray<__kindof?UIViewController?*>?*viewControllers; //設置隱藏導航欄 @property(nonatomic,getter=isNavigationBarHidden)?BOOL?navigationBarHidden; -?(void)setNavigationBarHidden:(BOOL)hidden?animated:(BOOL)animated; //導航欄對象,只讀屬性 @property(nonatomic,readonly)?UINavigationBar?*navigationBar; //隱藏狀態欄 @property(nonatomic,getter=isToolbarHidden)?BOOL?toolbarHidden?NS_AVAILABLE_IOS(3_0); -?(void)setToolbarHidden:(BOOL)hidden?animated:(BOOL)animated; //狀態欄對象 @property(null_resettable,nonatomic,readonly)?UIToolbar?*toolbar; //導航中的返回手勢對象 //iOS7之后,在導航中右劃會進行pop操作,設置這個的enable可以控制設置手勢是否失效 @property(nullable,?nonatomic,?readonly)?UIGestureRecognizer?*interactivePopGestureRecognizer; |
四、iOS8后導航的新特性
? | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 | //這個方法是為了iOS方法的命名統一,在導航中,其作用和push一樣 -?(void)showViewController:(UIViewController?*)vc?sender:(nullable?id)sender; //彈出鍵盤的時候隱藏導航欄 @property?(nonatomic,?readwrite,?assign)?BOOL?hidesBarsWhenKeyboardAppears; //屏幕滑動的時候隱藏導航欄,常用于tableView,上滑隱藏導航欄,下滑顯示,帶動畫效果 @property?(nonatomic,?readwrite,?assign)?BOOL?hidesBarsOnSwipe; //滑動隱藏導航欄的手勢 @property?(nonatomic,?readonly,?strong)?UIPanGestureRecognizer?*barHideOnSwipeGestureRecognizer; //橫屏的時候隱藏導航欄 @property?(nonatomic,?readwrite,?assign)?BOOL?hidesBarsWhenVerticallyCompact; //敲擊屏幕可以隱藏與顯示導航欄 @property?(nonatomic,?readwrite,?assign)?BOOL?hidesBarsOnTap; //敲擊屏幕的手勢 @property?(nonatomic,?readonly,?assign)?UITapGestureRecognizer?*barHideOnTapGestureRecognizer; |
iOS8中增加的這些方法,不得不說著實在用戶體驗生進了一大步,從中也可以看出apple對于用戶體驗度的用心。
五、UINavigationDelegate
????????導航控制器還提供了一些代理回調方法,如下:
| 1 2 3 4 5 6 7 8 9 10 11 | //視圖將要展示時調用的方法 -?(void)navigationController:(UINavigationController?*)navigationController?willShowViewController:(UIViewController?*)viewController?animated:(BOOL)animated; //視圖已經展示時調用的方法 -?(void)navigationController:(UINavigationController?*)navigationController?didShowViewController:(UIViewController?*)viewController?animated:(BOOL)animated; //設置方法設置導航控制器支持的設備方向 -?(UIInterfaceOrientationMask)navigationControllerSupportedInterfaceOrientations:(UINavigationController?*)navigationController?NS_AVAILABLE_IOS(7_0); //這個方法設置導航控制器的首選設備方向 -?(UIInterfaceOrientation)navigationControllerPreferredInterfaceOrientationForPresentation:(UINavigationController?*)navigationController?NS_AVAILABLE_IOS(7_0); //下面兩個方法可以對導航的轉場動畫進行設置 -?(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相關
????????當一個controller被添加到導航中后,系統會為它分配一些屬性,如下:
?| 1 2 3 4 5 6 | //當前controller對應的導航項 @property(nonatomic,readonly,strong)?UINavigationItem?*navigationItem; //push的時候隱藏底部欄,如push后隱藏tabbar @property(nonatomic)?BOOL?hidesBottomBarWhenPushed; //管理它的導航控制器 @property(nullable,?nonatomic,readonly,strong)?UINavigationController?*navigationController; |
? ??
總結
以上是生活随笔為你收集整理的iOS开发UINavigation系列四——导航控制器UINavigationController的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: WDS使用捕获映像制作企业自定义映像
- 下一篇: mysql-data-dumper