UINavigationController是用于构建分层应用程序的主要工具,主要采用栈形式来实现视图。 任何类型的视图控制器都可放入栈中。在设计导航控制器时需要指定根视图即用户看到的第一个视图。根视图控制器是被导航控制器推入到栈中的第一个视图控制 器。当用户查看下一个试图时,栈中将加入一个新的视图控制器,它所控制的视图将展示给用户。我们可以通过导航按钮来操作分层的应用程序,用它来控制视图的 推入或推出。
1、首先基本的属性
代码展示:
- (void)viewDidLoad {
NSLog(@"踹你啦");
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor redColor];
//导航栏的标题 上下的标题都变
// self.title = @"第一页";
//只有导航栏的标题改变
self.navigationItem.title = @"导航栏";
//导航栏的背景色,如果设置颜色,模糊效果就会消失
// self.navigationController.navigationBar.barTintColor = [UIColor magentaColor];
//模糊效果(透明度)
/*
模糊效果打开:视图的坐标原点是屏幕的左上角
模糊效果关闭:视图的坐标原点是导航栏的左下角
导航栏高度(包含状态栏)64 状态栏高度是 20
*/
self.navigationController.navigationBar.translucent = NO ;
//导航栏的背景图片
//64 包括状态栏
// [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"NavBar_64.png"] forBarMetrics:UIBarMetricsDefault];
//
// // 44 不包括状态栏
// [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"NavBar_44.png"] forBarMetrics:UIBarMetricsDefault];
//
// // 30 平铺 铺满导航栏和状态栏
// [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"NavBar_30.png"] forBarMetrics:UIBarMetricsDefault];
//self.navigationItem 每个页面显示在导航栏上的信息
//系统自带的样式·
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:@selector(itemAction:)];
//自定义文字标题的BarButtonItem
// self.navigationItem.leftBarButtonItem =[[UIBarButtonItem alloc]initWithTitle:@"点击" style:UIBarButtonItemStylePlain target:self action:@selector(itemAction:)];
//自定义多个BarButtonItem 加在leftItem
// UIBarButtonItem *leftItem1 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(itemAction:)];
//
// UIBarButtonItem *leftItem2 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(itemAction:)];
//
// //同时添加多个BarButtonItem
// self.navigationItem.leftBarButtonItems = @[leftItem1,leftItem2];
//BarButtonItem显示自定义图片 矢量图片
//渲染为模板
// self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"iconfont-card-1.png"] style:UIBarButtonItemStylePlain target:self action:@selector(rightAction:)];
//
//渲染为原图 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[[UIImage imageNamed:@"iconfont-card-1.png"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(rightAction:)];
//中间的titleView
self.navigationItem.titleView = [[UISegmentedControl alloc]initWithItems:@[@"王尼玛",@"什么鬼"]];
//导航栏上的原色颜色
self.navigationController.navigationBar.tintColor = [UIColor redColor];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
btn.frame = CGRectMake(0, 0, 60, 30);
btn.backgroundColor = [UIColor greenColor];
[btn setTitle:@"下一页" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
}
-(void)rightAction:(UIBarButtonItem *)itemButton{
}
-(void)itemAction:(UIBarButtonItem *)itemButton{
}
-(void)buttonAction:(UIButton *)button{
SecondViewController *secondVC = [[[SecondViewController alloc]init]autorelease];
//导航控制器推出下个页面 推出就是一个入栈的过程
[self.navigationController pushViewController:secondVC animated:YES];
}