iOS UINavigationController 详解

UINaviGationController 通常被称为导航栏,是视图与视图之间联系沟通的桥梁,它是容器视图控制器的一种,称之为导航视图控制器,导航视图控制器固定高度是 64,导航视图控制器中存放的是视图控制器其颜色与状态条相同


1> navigationController 的创建
在 AppDelegate 中
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    RootViewController *root = [[RootViewController alloc] initWithNibName:@"RootViewController" bundle:nil];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:root]; // 相当于将 RootViewController 对象放到 navigationController 容器中,现在容器中只有一个视图控制器
    [self.window setRootViewController:nav]; // 将 UINavigationController 做为 window 的根视图控制器
    [self.window makeKeyWindow];

    return YES;
}

2> navigationBar 和 navigationItem 导航条的设置
    <1> navigationBar 属性是属于 navigationController 的,不是某个 viewController 的,在一个 viewController 中设置,其他的 viewController 的导航条也会改变
    <2> 导航条的设置除 appearance 外都在 viewController 中完成设置
    <3> navigationItem 属性不是公有的,是每个 ViewController 都有一个自己 navigationItem,设置自己界面上的 navigationItem 属性不会影响其他的 viewController 界面
    <4> 导航栏的设置在 viewController 中完成设置,不设置左侧按钮(leftBarButtonItem)时,系统会自动以上一个视图控制器的标题作为左侧按钮,并自带返回响应事件
在 RootViewController 中
- (void)viewDidLoad
{
    [super viewDidLoad];

    self.title = @"test Title"; // 设置navigationbar上显示的标题
    [self.navigationController.navigationBar setTranslucent:NO]; // 设置navigationbar的半透明 NO:关闭,关闭时导航控制器下方视图的 y 坐标为 64,打开时为 20,默认打开
    NSDictionary *dict = [NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:NSForegroundColorAttributeName];
    [self.navigationController.navigationBar setTitleTextAttributes:dict]; // 设置navigationbar的字体颜色
    [self.navigationController.navigationBar setBarTintColor:[UIColor purpleColor]]; // 设置navigationbar的背景颜色
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:nil]; // 设置navigationbar左边按钮
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:nil]; // 设置navigationbar右边按钮
    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]]; // 设置navigationbar上左右按钮字体颜色

    self.navigationController.navigationBarHidden = YES; // 设置是否隐藏导航视图控制器,显示时导航控制器下方视图的 y 坐标为 64,隐藏时为 20
    ...
}

3> 页面切换
    <1> 跳转到新页 : 推出新页,自动将 otherViewController 添加到 navigationController 容器中
[self.navigationController pushViewController:otherViewController animated:YES];
    <2> 返回上一页
[self.navigationController popViewControllerAnimated:YES];
    <3> 返回到指定页 : viewController 添加在容器视图中的顺序已知
[self.navigationController popToViewController:self.navigationController.viewControllers[1] animated:YES];
    <4> 返回到指定页 : viewController 添加在容器视图中的顺序未知
for (UIViewController *viewController in self.navigationController.viewControllers)
{
    if ([viewController isKindOfClass:[ViewController3 class]])
    {
        // 回到指定的某一页
        [self.navigationController popToViewController:viewController animated:YES];
    }
}
    <5> 返回到根视图
[self.navigationController popToRootViewControllerAnimated:YES];

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值