定义篇
1、**UINavigationController是用于构建层级应用的主要工具。
**与UITabBarController在管理以及互相切换各个内容视图的方式类似。
**两者的主要区别在于UINavigationController是作为栈来实现的,因此非常适用于处理层级结构。
2、导航控制器维护着一个视图控制器栈。在设计导航控制器时,需要制定用户看到的第一个视图——根视图控制器(或简称根控制器),它也是导航控制器栈中的所有视图控制器的栈底。当用户选择查看下一个视图时,栈中将插入一个新的视图控制器由她控制的内容也会显示出来。这些新的视图控制器则被称为子控制器。
3、只有位于栈顶的视图控制器对象,其视图才是可见的。
4、我们利用导航栏控制器栈顶层视图控制器的title属性值来定义导航栏的标题,之前视图控制器的title属性值来定义返回按钮的标题。
5、当用户点击返回按钮的时候,当前的视图控制器就会被推出栈,之前的视图变为当前视图。
6、这种设计模式,每个控制器只需要知道其子控制器,以便在用户操作时把相应的新控制器对象加入栈中。
7、所添加的子视图不要放置在view的顶端,在视图控制器中,view会衬于UINavigationBar的下方,导致view被遮挡(UITabBar的view也不应该放置在view的最底端)
8、使用UINavigationController时,不能简单地把所有可能用到的视图控制器都压进UINavigationController对象的栈。该对象的数组是动态的,一开始只有一个根视图控制器,要根据情况来推进新的视图控制器。
9、当视图控制器进行切换时,将要出栈的视图控制器对象会收到viewWillDisappear:消息,
而将要入栈的视图控制器对象则收到viewWillAppear:消息。
10、某个非UINavigationController(NC)的对象来创建一个新的VC对象,并将新创建的VC对象压入NC对象的栈。
而这个负责创建VC对象的对象,需要满足:
条件一、知道什么时候把VC对象压入栈。
条件二、拥有一个指向NC对象的指针,以便向NC对象发送pushViewController:animated:消息。
1、导航栏的标题
//1.1、
//导航栏标题
UILabel *titleLabel=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 44)];
titleLabel.backgroundColor=[UIColor clearColor];//背景颜色
titleLabel.font=[UIFont boldSystemFontOfSize:18];//字体大小
titleLabel.textColor=[UIColor whiteColor];//字体颜色
titleLabel.text=@"笔记本";//文本
self.navigationItem.titleView=titleLabel;//标题栏获取文本
//1.2
//1.2.1、
//设置导航标题
[self.navigationItem setTitle:@"笔记本"];
//
//1.2.2、标题的背景颜色
[self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],NSForegroundColorAttributeName,nil]];
//
2、在navigationBar上添加按钮leftBarButtonItem(代码式)
//初始化一个按钮
leftBackButton=[UIButton buttonWithType:UIButtonTypeSystem];
leftBackButton.frame=CGRectMake(0, 0, 100,25);
//按钮文本
[leftBackButton setTitle:@"返回" forState:UIControlStateNormal];
//按钮文本字体大小
leftBackButton.titleLabel.font=[UIFont systemFontOfSize:18];
//
self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]initWithCustomView:leftBackButton];
//监听按钮
[leftBackButton addTarget:self action:@selector(leftBarButtonClick:) forControlEvents:UIControlEventTouchUpInside];
3、在navigationBar上添加一个(自定义)按钮
//
//初始化一个按钮
addNoteBookButton=[UIButton buttonWithType:UIButtonTypeSystem];
addNoteBookButton.frame=CGRectMake(300, 0, 100,25);
[addNoteBookButton setTitle:@"+" forState:UIControlStateNormal];//按钮的文本
addNoteBookButton.titleLabel.font=[UIFont systemFontOfSize:35];//按钮文本大小
[addNoteBookButton setTintColor:[UIColor whiteColor]];//文本颜色
[self.navigationController.navigationBar addSubview:addNoteBookButton];//加在自定义地点
//或(1、)
self.navigationItem.titleView=addNoteBookButton;//加在title的位置
//或(2、)
self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithCustomView:addNoteBookButton];//加在自定义地点
//以下方法则无效
[self.view addSubview:addNoteBookButton];//无效
4、创建一个系统自带式样的button
//这样创建的按钮,item的图片和大小都是固定的
self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addNoteBookButtonClick:) ];
//隐藏方法(没有hidden和alpha属性)
self.navigationItem.rightBarButtonItem=nil;
//再次显示的方法是重新加载
5、附带UIBarButtonSystemItem的样式
其中,无式样的两个 UIBarButtonSystemItemFlexibleSpace 和 UIBarButtonSystemItemFixedSpace并不是按钮,而是调整按钮间距用的对象.
6、将一个新创建的VC对象压入NC对象的栈
//假设已经创建了BNRItemsViewController和BNRDetailViewController这两个UIViewController的子类
@implementation BNRItemsViewController
//当用户点击UITableView中的某一行时
-(void)tableView: (UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
BNRDetailViewController *detailViewController=[[BNRDetailViewController alloc]init];
//压入栈
[self.navigationController pushViewController:detailViewController animated:YES];
}
7、(推荐)改变系统自带的返回按钮字样
设置navigationController中返回按钮的标题,默认为前一个视图中标题的title(backBarButtonItem属性是对应于本视图控制器的下一级控制器的返回按钮,区别于leftBarButtonItem)。
//设置返回键:
//在第一级页面的viewDidLoad方法中加入以下代码(相当于用一个新的按钮在进行导航前将原来的返回按钮替换掉)
UIBarButtonItem *resetBackItem=[[UIBarButtonItem alloc]init];
resetBackItem.title=@"返回";
self.navigationItem.backBarButtonItem=resetBackItem;
8、(推荐)设置一个按钮
//设置并监听按钮
aButton=[[UIBarButtonItem alloc]initWithTitle:@"..." style:UIBarButtonItemStylePlain target:self action:@selector(aButtonClick:)];
//添加按钮
self.navigationItem.rightBarButtonItem=aButton;
//设置字体的大小(对backBarButtonItem、leftBarButtonItem都可以使用)
[self.navigationItem.rightBarButtonItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont boldSystemFontOfSize:35],NSFontAttributeName,nil] forState:UIControlStateNormal];
//设置字体颜色
[self.navigationItem.rightBarButtonItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor greenColor],NSForegroundColorAttributeName, nil] forState:UIControlStateNormal];
参考链接:iOS系统自带控件 UIBarButtonSystemItem 的样式解析
修改navigationItem.backBarButtonItem.title的值无效的解决办法
修改UIBarButtonItem字体大小、颜色等相关属性
UIBarButtonItem(苹果官网)