iOS 技术

UINavigationController 导航控制器

2016-02-27  本文已影响633人  我是滕先生

概念

总结:

NavigationController管理的东西:
  1. NavigationController管理多个Controller
  2. NavigationController管理着NavigationBar(通过navigationController. navigationBar方式可以调用)
    NavigationBar管理多个NavigationItem,和NavigationController一样都是用push和pop的方式推进推出各自管理的东西NavigationItem包含多个UIBarButtonItem
  3. NavigationController管理着ToolBar,通过navigationController.toolbar方式调用
控制器管理的东西

每个界面的Controller管理一个它界面的NavigationItem(通过controller. navigationItem方式可以调用)


属性

1、获取栈顶的controller

@property(nullable, nonatomic,readonly,strong) UIViewController *topViewController;

2、获取当前界面显示的controller

@property(nullable, nonatomic,readonly,strong) UIViewController *visibleViewController;

3、 给导航控制器设置controller

@property(nonatomic,copy) NSArray<__kindof UIViewController *> *viewControllers;

例子: //将上面的这5个控制器添加到控制器数组中

self.viewControllers = @[navHall, navArena, navDiscovery, navHistory, navMyLottery];

4、设置隐藏导航栏

@property(nonatomic,getter=isNavigationBarHidden) BOOL navigationBarHidden;

5、 获取导航栏

@property(nonatomic,readonly) UINavigationBar *navigationBar;

6、是否隐藏底部工具条(默认隐藏)

@property(nonatomic,getter=isToolbarHidden) BOOL toolbarHidden;

7、获取底部工具条

@property(null_resettable,nonatomic,readonly) UIToolbar *toolbar;

8、获取导航中的返回手势对象(iOS7之后,在导航中右划会进行pop操作,设置这个的enable可以控制设置手势是否失效)

@property(nullable, nonatomic, readonly) UIGestureRecognizer *interactivePopGestureRecognizer;

9、获取滑动隐藏导航栏的手势

@property (nonatomic, readonly, strong) UIPanGestureRecognizer *barHideOnSwipeGestureRecognizer;

10、屏幕滑动的时候是否隐藏导航栏,常用于tableView,上滑隐藏导航栏,下滑显示,带动画效果

@property (nonatomic, readwrite, assign) BOOL hidesBarsOnSwipe;

11、 弹出键盘的时候是否隐藏导航栏

@property (nonatomic, readwrite, assign) BOOL hidesBarsWhenKeyboardAppears;

12、设置横屏的时候隐藏导航栏

@property (nonatomic, readwrite, assign) BOOL hidesBarsWhenVerticallyCompact;

13、敲击屏幕可以隐藏与显示导航栏

@property (nonatomic, readwrite, assign) BOOL hidesBarsOnTap;

14、获取敲击屏幕的手势

@property (nonatomic, readonly, assign) UITapGestureRecognizer *barHideOnTapGestureRecognizer;

15、导航控制器代理

@property(nullable, nonatomic, weak) id<UINavigationControllerDelegate> delegate;

16、当一个controller被添加到导航中后,系统会为它分配一些属性
(1)当前controller对应的导航项

@property(nonatomic,readonly,strong) UINavigationItem *navigationItem;

(2)push的时候隐藏底部栏,如push后隐藏tabbar

@property(nonatomic) BOOL hidesBottomBarWhenPushed;

(3)获取管理它的导航控制器

@property(nullable, nonatomic,readonly,strong) UINavigationController *navigationController;

方法

1、创建导航控制器的两种方法
(1)通过一个自定义的导航栏和工具栏创建导航控制器

- (instancetype)initWithNavigationBarClass:(nullable Class)navigationBarClass toolbarClass:(nullable Class)toolbarClass;

(2)使用系统默认的导航栏和工具栏,创建一个导航控制器同时设置一个根视图控制器

- (instancetype)initWithRootViewController:(UIViewController *)rootViewController;

2、设置管理的视图控制器

- (void)setViewControllers:(NSArray<UIViewController *> *)viewControllers animated:(BOOL)animated;

3、管理视图控制器操作
(1)压入新的视图控制器

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated;

这个方法是为了iOS方法的命名统一,在导航中,其作用和push一样

- (void)showViewController:(UIViewController *)vc sender:(nullable id)sender;

例子:设置隐藏底部TabBar
可以给所有控制器设置一个父类,重写该方法。继承该父类的控制器调用此方法都可以隐藏push来的控制器底部的TabBar

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
    // 这里的参数viewController就是即将被push进来的控制器
    viewController.hidesBottomBarWhenPushed = YES;   
 [super pushViewController:viewController animated:animated];
}

(2)弹出一个视图控制器 返回的是pop的controller

- (nullable UIViewController *)popViewControllerAnimated:(BOOL)animated;

(3)弹出到某个视图控制器 返回所有pop的controller

- (nullable NSArray<__kindof UIViewController *> *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated;

(4)直接pop到根视图控制器,返回所有被pop的controller

- (nullable NSArray<__kindof UIViewController *> *)popToRootViewControllerAnimated:(BOOL)animated;

4、设置隐藏导航栏

- (void)setNavigationBarHidden:(BOOL)hidden animated:(BOOL)animated;

5、是否隐藏底部工具条(默认隐藏)

- (void)setToolbarHidden:(BOOL)hidden animated:(BOOL)animated;

6、代理方法:
(1)视图将要展示时调用的方法

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated;

(2)视图已经展示时调用的方法

- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated;

(3)设置导航控制器支持的设备方向

- (UIInterfaceOrientationMask)navigationControllerSupportedInterfaceOrientations:(UINavigationController *)navigationController;

(4)设置导航控制器的首选设备方向

- (UIInterfaceOrientation)navigationControllerPreferredInterfaceOrientationForPresentation:(UINavigationController *)navigationController;

(5)下面两个方法可以设置导航的转场动画

- (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 ;
上一篇下一篇

猜你喜欢

热点阅读