前言:
这是一篇小白的技术博客,写下来一是为了整理,二是为了分享。就从UI基础开始写起吧。另,初次执笔,难免言辞羞涩,请各位看官包容指教。
一.首先让我们来认识UINavigationController(导航控制器)
看一下Xcode里的Description:
The UINavigationController class implements a specialized view controller that manages the navigation of hierarchical content. This navigation interface makes it possible to present your data efficiently and makes it easier for the user to navigate that content. You generally use this class as-is but in iOS 6 and later you may subclass to customize the class behavior.
说白了 导航控制器就是管理视图控制器的工具。
1.看一下官方文档给出的样子:
A sample navigation interface
这样就清楚UINavigationController是什么东东了吧。这里要指出的是NavigationController是以栈的方式管理它的视图控制器的,视图控制器的弹入弹出都是入栈出栈的过程,导航栈就相当于存放ViewController的数组。官方也有说明:
A navigation controller object manages the currently displayed screens using the navigation stack, which is represented by an array of view controllers.
2.看一下NavigationController和他管理的对象之间的关系
Objects managed by the navigation controller
- NavigationController以导航栈来存储ViewController。
- navigationBar一直存在于界面上方,由NavigationController根据navigation stack中栈顶的ViewController的内容来管理。
- toolbar则存在于界面下方且默认为隐藏,同样由NavigationController根据navigation stack中栈顶的ViewController的内容来管理。
3.看一下NavigationController里的视图
The views of a navigation controller
-
NavigationController是一个ViewController容器,它把其他的ViewController的内容嵌套在容器里。 你能看到一个NavigationController的view,它包含了navigation bar、toolbar和一个相当于栈顶ViewController的content view。
-
NavigationController允许定制navigationBar但是不能修改直接它的frame, bounds和alpha值,如果你定制了NavigationBar,你必须使用下面的方法来initialized。
initWithNavigationBarClass:toolbarClass: -
请注意上图的层次关系。
关于NavigationBar
1.The Left Item
- 对于新推出的ViewController,如果有自定义的 leftBarButtonItem,那么就展示自定义的item。
- 如果没有自定义left item,那么会自动添加一个默认的backBarButtonItem,其title默认为上一级ViewController的title。功能是返回上一级NavigationController。
- 如果自定义的bar button item没有特别说明,那么会默认使用一个back button,其title也是由上一级ViewController决定的(如果只有一个ViewController在navigation stack里, 没有返回按钮)
2.The Middle Item
- 对于新推出的ViewController,如果有自定义的 title view,那么navigation bar就将这个title view放到默认的title view位置。
- 如果没有自定义title view,那么navigation bar就会放置一个包含ViewController的title的label,label上的字符串通常会取自ViewController的title属性,如果你想展示一个不同的title,你可以设置ViewController的 navigation item的属性。
3.The Right Item
- 对于新推出的ViewController,如果有自定义的 right bar button item,那么就展示这个item,设为rightBarButtonItem。
- 如果没有自定义 right bar button就什么也没有。
二.代码学习UINavigationController
// 初始化UINavigationController
ViewController *VC = [[ViewController alloc] init];
UINavigationController *rootNC = [[UINavigationController alloc] initWithRootViewController:VC];
self.window.rootViewController = rootNC;
toolbar的属性
// 显示toolbar
rootNC.toolbarHidden = NO;
navigationBar的属性
// 隐藏navigationBar
rootNC.navigationBarHidden = YES;
// navigationBar颜色
rootNC.navigationBar.tintColor = [UIColor cyanColor];
// navigationBar的类型
rootNC.navigationBar.barStyle = UIBarStyleBlack;
// navigationBar是否透明
rootNC.navigationBar.translucent = NO;
// navigationBar的背景图片
rootNC.navigationBar setBackgroundImage:<#(UIImage *)#> forBarMetrics:<#(UIBarMetrics)#>
item
// 自定义The Left Item
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
[button setTitle:@"返回" forState:UIControlStateNormal];
button.backgroundColor = [UIColor grayColor];
[button addTarget:self action:@selector(popLastViewController) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.leftBarButtonItem = item;
// 自定义The Middle Item
UIButton *button = [UIButton buttonWithType:UIButtonTypeContactAdd];
self.navigationItem.titleView = button;
// 自定义The Right Item
UIButton *button = [UIButton buttonWithType:UIButtonTypeContactAdd];
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.rightBarButtonItem = item;
// 自定义The Right Item数组
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeContactAdd];
UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithCustomView:button1];
UIButton *button2 = [UIButton buttonWithType:UIButtonTypeInfoDark];
UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithCustomView:button2];
UIButton *button3 = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
[button3 setTitle:@"right" forState:UIControlStateNormal];
button3.backgroundColor = [UIColor grayColor];
UIBarButtonItem *item3 = [[UIBarButtonItem alloc] initWithCustomView:button3];
self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:item1, item2, item3, nil];
push方法
// push到指定视图控制器
- (void)pushNextController {
FirstViewController *firstVC = [[FirstViewController alloc] init];
[self.navigationController pushViewController:firstVC animated:YES];
}
pop方法
// pop到上一级视图控制器
- (void)popController {
[self.navigationController popViewControllerAnimated:YES];
}
// pop到指定视图控制器
- (void)popSomeoneController {
// 注意:这里不能alloc,因为SecondViewController已经在栈里了,要从堆栈中取索引为1的Controller(即SecondViewController)
SecondViewController *secondVC = (SecondViewController *)[self.navigationController.viewControllers objectAtIndex:1];
[self.navigationController popToViewController:secondVC animated:YES];
}
// pop到根视图控制器
- (void)popRootController {
[self.navigationController popToRootViewControllerAnimated:YES];
}
看到这里想必大家对UINavigationController相当熟悉了吧。还有一些属性、方法、代理,自己到文档里探索学习吧,看,so easy!
- UINavigationBar的属性
- UIToolbar的属性
- UINavigationItem
- UINavigationControllerDelegate
后记
小白出手,请多指教。
如言有误,还望斧正!
- 转载请保留原文地址:http://www.jianshu.com/p/5b674332efb6
- 有兴趣的读者欢迎关注我的微博:与佳期
- 本章官方文档:UINavigationController(推荐阅读)











网友评论