UINavigationController API

本文详细介绍了iOS中的UINavigationController组件,包括其基本概念、创建方法、导航堆栈操作、导航栏及工具栏配置等,并提供了相关代理方法说明。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

UINavigationController
简介

iOS中的导航控制器,概念和数据结构里的栈类似,导航控制器使用了导航堆栈。根视图控制器在堆栈最底层,可以将视图控制器推入栈顶。
创建导航控制器

//便利初始化方法,同时设置根视图控制器
- (instancetype)initWithRootViewController:(UIViewController *)rootViewController

//在导航控制器中使用定制的导航栏和工具栏子类。
- (instancetype)initWithNavigationBarClass:(Class)navigationBarClass
                              toolbarClass:(Class)toolbarClass

访问导航堆栈项目

//代表当前navigation栈中最上层的VC,只读属性
@property(nonatomic, readonly, retain) UIViewController *topViewController

//代表当前可见的VC,它可能是topViewController,也可能是当前topViewController present出来的VC
@property(nonatomic, readonly, retain) UIViewController *visibleViewController

//导航栈里所有的控制器
@property(nonatomic, copy) NSArray *viewControllers

//构造整个VC栈并应用到当前的UINavigationController中
- (void)setViewControllers:(NSArray *)viewControllers
                  animated:(BOOL)animated

压入和弹出导航栈按钮

//推出一个视图控制器,相当于将这个控制器压入导航栈
- (void)pushViewController:(UIViewController *)viewController
                  animated:(BOOL)animated

//返回上一个控制器,相当于将这个控制器出栈
- (UIViewController *)popViewControllerAnimated:(BOOL)animated

//返回到根视图控制器
- (NSArray *)popToRootViewControllerAnimated:(BOOL)animated

//返回到某个指定的控制器
- (NSArray *)popToViewController:(UIViewController *)viewController
                        animated:(BOOL)animated

//返回上一个控制器的手势
@property(nonatomic, readonly) UIGestureRecognizer *interactivePopGestureRecognizer

配置导航栏

//获取导航栏
@property(nonatomic, readonly) UINavigationBar *navigationBar

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

隐藏导航栏

//默认全是NO

//是否允许使用轻击手势隐藏导航栏
@property(nonatomic, readwrite, assign) BOOL hidesBarsOnTap

//是否允许使用扫动手势隐藏导航栏
@property(nonatomic, readwrite, assign) BOOL hidesBarsOnSwipe

//是否在垂直方向很窄的时候隐藏导航栏
@property(nonatomic, readwrite, assign) BOOL hidesBarsWhenVerticallyCompact

//是否在弹出键盘的时候隐藏导航栏
@property(nonatomic, readwrite, assign) BOOL hidesBarsWhenKeyboardAppears

//隐藏导航栏
@property(nonatomic, getter=isNavigationBarHidden) BOOL navigationBarHidden

//获取隐藏导航栏的手势识别器
@property(nonatomic, readonly, assign) UITapGestureRecognizer *barHideOnTapGestureRecognizer

委托

@property(nonatomic, assign) id< UINavigationControllerDelegate > delegate

配置自定义工具栏

//工具栏
@property(nonatomic, readonly) UIToolbar *toolbar

//是否隐藏工具栏
- (void)setToolbarHidden:(BOOL)hidden
                animated:(BOOL)animated

//工具栏的隐藏属性
@property(nonatomic, getter=isToolbarHidden) BOOL toolbarHidden

显示视图控制器的方法

//如果在导航栈中,就push,否则就以模态视图显示
- (void)showViewController:(UIViewController *)vc
                    sender:(id)sender

常数

//导航栏隐藏显示的动画时间
extern const CGFloat UINavigationControllerHideShowBarDuration

UINavigationControllerDelegate

//导航控制器将要显示导航栏的时候
- (void)navigationController:(UINavigationController *)navigationController
      willShowViewController:(UIViewController *)viewController
                    animated:(BOOL)animated

////导航控制器已经显示导航栏的时候
- (void)navigationController:(UINavigationController *)navigationController
       didShowViewController:(UIViewController *)viewController
                    animated:(BOOL)animated

自定义过渡动画

//返回控制器切换时的交互式动画对象
- (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController
                                  animationControllerForOperation:(UINavigationControllerOperation)operation
                                               fromViewController:(UIViewController *)fromVC
                                                 toViewController:(UIViewController *)toVC

注意

- (id<UIViewControllerInteractiveTransitioning>)navigationController:(UINavigationController *)navigationController
                         interactionControllerForAnimationController:(id<UIViewControllerAnimatedTransitioning>)animationController

- (UIInterfaceOrientation)navigationControllerPreferredInterfaceOrientationForPresentation:(UINavigationController *)navigationController

- (NSUInteger)navigationControllerSupportedInterfaceOrientations:(UINavigationController *)navigationController

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值