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

iOS UINavigationController 详解

2017-01-07 21:25 281 查看
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];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  IOS objective-c