UINavigationController简译及简解

2015-10-11  本文已影响425人  与佳期

前言:

这是一篇小白的技术博客,写下来一是为了整理,二是为了分享。就从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
3.看一下NavigationController里的视图
The views of a navigation controller

关于NavigationBar

1.The Left Item
2.The Middle Item
3.The Right Item

二.代码学习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!

后记

小白出手,请多指教。
如言有误,还望斧正!

上一篇 下一篇

猜你喜欢

热点阅读