在前面的博客中,我麼你介紹了UINavigationBar,UINavigationItem和UIToolBar,UINavigationController是將這些控件和UIViewController緊密的結合了起來,使用導航,咱們的應用程序層次會更加分明,對controller的管理也更加方便。前幾篇博客地址以下:app
UINavigationBar:http://my.oschina.net/u/2340880/blog/527706ide
UINavigationItem:http://my.oschina.net/u/2340880/blog/527781工具
UIToolBar:http://my.oschina.net/u/2340880/blog/528168動畫
導航控制器是一個堆棧結構,只是其中管理的對象是controller,經過push與pop進行controller的切換,咱們有兩種方式能夠建立導航控制器:atom
//經過一個自定義的導航欄和工具欄建立導航控制器 - (instancetype)initWithNavigationBarClass:(nullable Class)navigationBarClass toolbarClass:(nullable Class)toolbarClass; //使用系統默認的導航欄和工具欄,經過一個根視圖建立導航控制器 - (instancetype)initWithRootViewController:(UIViewController *)rootViewController;
經過如下方法對視圖控制器進行管理操做:spa
//設置管理的視圖控制器 - (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;
//返回棧頂的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。.net
//返回堆棧中全部的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;
//這個方法是爲了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對於用戶體驗度的用心。代理
導航控制器還提供了一些代理回調方法,以下:code
//視圖將要展現時調用的方法 - (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 ;
當一個controller被添加到導航中後,系統會爲它分配一些屬性,以下:對象
//當前controller對應的導航項 @property(nonatomic,readonly,strong) UINavigationItem *navigationItem; //push的時候隱藏底部欄,如push後隱藏tabbar @property(nonatomic) BOOL hidesBottomBarWhenPushed; //管理它的導航控制器 @property(nullable, nonatomic,readonly,strong) UINavigationController *navigationController;
專一技術,熱愛生活,交流技術,也作朋友。
——琿少 QQ羣:203317592