您的位置:首页 > 产品设计 > UI/UE

UINavigationController 基础篇

2016-03-28 15:08 555 查看
UINavigationController是管理堆栈视图和导航栏的工具,在每个UINavigationController栈内部的viewcontroller都会有一个自己对应的navigationItem.如果UINavigationController是嵌套在UITabbar上面,那么它使用标题和底部的工具栏都会在堆栈上。同时UINavigationController也是一个可旋转的视图控件。

UINavigationController属性:

topViewController:顶部视图 (在UINavigationController堆栈中所有视图控制器中最顶上的一个视图控制器,只读属性)

visibleViewController:当前视图 (当前可见的视图控制器,可能是topViewController也有可能是topViewController推出来一个新的视图控制器,只读属性)

viewControllers :视图控制器集合(返回堆栈中的所有视图控制器,可以对这些控制器进行操作)

navigationBarHidden:隐藏导航栏

navigationBar :导航栏对象(有自己的样式范围大小等,一般情况下是基于navigationcontroller来使用的。可以和navigationItem一起理解,navigationBar在navigationItem之下用于显示整体的颜色样式,而navigationItem则是用于控制上面的左,右,返回按钮及标题)

toolbarHidden:标签栏隐藏 (默认请款下是:YES隐藏,NO为不隐藏。不要和toolbar.hidden属性混合)

toolbar:标签栏 (一般情况下当做一个展示表单,存放各种button)

interactivePopGestureRecognizer:设置右滑出栈手势 (取值为UIGestureRecognizer是一个只读属性,iOS7加入)

self.navigationController.interactivePopGestureRecognizer.enabled = NO; (不允许右滑出栈)

self.navigationController.interactivePopGestureRecognizer.delegate = self; (设置代理UIGestureRecognizerDelegate)

也可用自己检测手势来响应:

[self.navigationController.interactivePopGestureRecognizeraddTarget:self action:@selector(handleGesture:)];

hidesBarsWhenKeyboardAppears:当键盘显示的时候导航栏消失 (但是键盘消失的时候导航栏仍然会被隐藏,状态栏不会消失,iOS8加入)

hidesBarsOnSwipe:滑动隐藏导航栏状态栏 (可以设置成上滑隐藏,下滑显示。iOS8加入)

barHideOnSwipeGestureRecognizer:滑动隐藏导航栏手势 (取值为UIPanGestureRecognizer,iOS8加入)

hidesBarsWhenVerticallyCompact:横屏的时候隐藏状态栏 (iOS8加入)

hidesBarsOnTap:点击屏幕隐藏显示导航栏和标签栏 (标签栏的状态取决于其自身状态时候有设定隐藏或者显示,iOS8加入)

barHideOnTapGestureRecognizer:获取点击隐藏导航栏的手势 (取值为UITapGestureRecognizer,iOS8加入)

navigationItem:导航项 (包括左按钮,右按钮,返回按钮,标题,prompt,等。在使用prompt的时候导航栏的高度回变成74)

hidesBottomBarWhenPushed:是否隐藏标签栏

toolbarItems:标签栏(可以通过自定义的方式存放按钮)

UINavigationController方法:

- (instancetype)initWithNavigationBarClass:(nullable Class)navigationBarClass toolbarClass:(nullable Class)toolbarClass;自定义导航栏和标签栏创建一个导航栏控制器。

- (instancetype)initWithRootViewController:(UIViewController *)rootViewController;通过设置根视图控制器的使用系统的导航栏创建导航栏视图控制器

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated;压入一个新的视图控制器可设置是否要动画效果

- (nullable UIViewController *)popViewControllerAnimated:(BOOL)animated; 弹窗一个视图控制器

- (nullable NSArray<__kindof UIViewController *> *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated; 弹出一个指定的视图控制器,返回导航栏控制器中的所有视图控制器

- (nullable NSArray<__kindof UIViewController *> *)popToRootViewControllerAnimated:(BOOL)animated;弹出根视图控制器,返回导航栏控制器中的所有视图控制器

- (void)setViewControllers:(NSArray<UIViewController *> *)viewControllers animated:(BOOL)animated 设置视图控制器和是否需要动画效果(模拟push和pop的操作)

- (void)setNavigationBarHidden:(BOOL)hidden animated:(BOOL)animated; 设置是否隐藏导航栏

- (void)setToolbarHidden:(BOOL)hidden animated:(BOOL)animated;设置是否隐藏标签栏

- (void)showViewController:(UIViewController *)vc sender:(nullable id)sender;效果和push方法一样,为了同意命名 (iOS8加入)

- (void)setToolbarItems:(nullable NSArray<UIBarButtonItem *> *)toolbarItems animated:(BOOL)animated;设置标签栏上面的按钮

UIBarButtonItem *one = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:nil action:nil];

UIBarButtonItem *two = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:nil action:nil];

UIBarButtonItem *three = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:nil action:nil];

UIBarButtonItem *four = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:nil action:nil];

UIBarButtonItem *flexItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];

[self setToolbarItems:[NSArray arrayWithObjects:flexItem, one, flexItem, two, flexItem, three, flexItem, four, flexItem, nil]];

代理方法:

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated; 在push或者pop操作的时候视图控制器将要显示的时候执行的方法

- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated;在push或者pop操作的时候视图控制器已经显示的时候执行的方法

- (UIInterfaceOrientationMask)navigationControllerSupportedInterfaceOrientations:(UINavigationController *)navigationController;设置导航栏控制器支持的手机的方向
(ios7加入)

- (UIInterfaceOrientation)navigationControllerPreferredInterfaceOrientationForPresentation:(UINavigationController
*)navigationController;设置导航控制器的首选设备方向

//两个方法在对屏幕进行翻转的时候对导航栏控制器变化的动画设置

- (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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: