//初始化导航控制器
ViewController *vc=[[ViewController alloc]init];
UINavigationController *nav=[[UINavigationController alloc]initWithRootViewController:vc];
self.window.rootViewController=nav;
一.导航控制器的基本组成
1.navigationBar 导航栏
2.toolbar 工具栏
二.导航控制器的基本属性和方法
//获取栈中最顶层的视图控制器
self.navigationController.topViewController
//获取栈中当前现实的视图控制器
self.navigationController.visibleViewController
//隐藏导航栏
self.navigationController.navigationBarHidden=YES;
//获取导航栏目
self.navigationController.navigationBar;
//设置导航栏隐藏
[self.navigationController setNavigationBarHidden:YES];
//设置工具栏隐藏
[self.navigationController setToolbarHidden:YES];
//设置标题
[self.navigationController setTitle:@"hello"];
//设置导航栏背景图片
[nav.navigationBar setBackgroundImage:[UIImage imageNamed:@"1.png"] forBarMetrics:UIBarMetricsDefault];
三.导航控制器页面跳转
// 跳转到下一个页面 push方法
SecondViewController *secondVC = [[SecondViewController alloc] init];
[self.navigationController pushViewController:secondVC animated:YES];
// 返回上一页
[self.navigationController popViewControllerAnimated:YES];
// 回到根视图
[self.navigationController popToRootViewControllerAnimated:YES];
//返回指定页面
NSArray *array = self.navigationController.viewControllers;
SecondViewController *secondVC = [array objectAtIndex:1];
[self.navigationController popToViewController:secondVC animated:YES];
四.导航栏和工具栏
(1)导航栏
1。导航栏添加按钮
//系统样式
UIBarButtonItem *right=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addNewOne)];
self.navigationItem.rightBarButtonItem=right;
/*
图片样式
UIBarButtonItem *right1=[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"1.png"] style:UIBarButtonItemStyleBordered target:self action:@selector(addNewOne)];
字样式
UIBarButtonItem *right=[[UIBarButtonItem alloc]initWithTitle:@"add" style:UIBarButtonItemStyleBordered target:self action:@selector(addNewOne)];
*/
2.设置导航栏
//设置导航栏的风格
self.navigationController.navigationBar.barStyle=UIBarStyleBlack;
//设置导航栏为透明
self.navigationController.navigationBar.translucent=YES;
//设置导航栏的颜色
self.navigationController.navigationBar.tintColor=[UIColor redColor];
//隐藏导航栏返回按钮
[self.navigationItem setHidesBackButton:YES];
//导航栏提示
[email protected]"hello";
3.定制导航栏
//view为自定义UIView
1.self.navigationItem.titleView=view;
(2)工具栏
1.初始化工具栏
//初始化toolbar
UIToolbar *toolbar=[[UIToolbar alloc]initWithFrame:CGRectMake(0, 450, 320, 30)];
//初始化toolbar上的按钮
UIBarButtonItem *button1=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(goToNext)];
//制作间隔(系统默认等宽间隔)
UIBarButtonItem *fiex=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
/*
//自定义间隔
UIBarButtonItem *myfiex=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:self action:nil];
myfiex.width=30;
*/
//将按钮放进数组中
NSArray *array=[[NSArray alloc]initWithObjects:button1 ,fiex,button2,fiex,button3, nil];
//将数组的按钮添加到toolbar中
[toolbar setItems:array animated:YES];
//将toolbar添加到view中
[self.view addSubview:toolbar];
五.导航控制器部件大小
状态栏 20px;
导航栏 44/32px;
工具栏 44/32px;
导航栏按钮 20*20;