UINavigationController是IOS編程中比較常用的一種容器view controller,也就是導(dǎo)航欄控制器。
1.創(chuàng)建一個(gè)UINavigationController
self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
FirstViewController * first = [[FirstViewController alloc]init];
UINavigationController * nav = [[UINavigationControlleralloc]initWithRootViewController:first]; //UINavigationController第一個(gè)顯示的界面
self.window.rootViewController = nav;
[self.window makeKeyAndVisible];
2.隱藏導(dǎo)航欄工具欄
self.navigationController.navigationBarHidden = YES;
self.navigationController.toolbarHidden = NO;
3.設(shè)置導(dǎo)航欄和工具欄的顏色(設(shè)置了之后所有都是這個(gè)顏色,因?yàn)橹挥幸粋€(gè)navigationController)
self.navigationController.toolbar.barTintColor = [UIColor redColor];
self.navigationController.navigationBar.barTintColor = [UIColor redColor];
4.設(shè)置左右顯示的按鈕
UIBarButtonItem * leftBtn = [[UIBarButtonItem alloc]initWithTitle:@"Cancel" style:UIBarButtonItemStyleDonetarget:nil action:nil];
self.navigationItem.leftBarButtonItem = leftBtn;
self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
UIBarButtonItem * rightBtn = [[UIBarButtonItem alloc]initWithTitle:@"Next" style:UIBarButtonItemStyleDonetarget:self action:@selector(gotoSecondView)];
self.navigationItem.rightBarButtonItem = rightBtn;
self.navigationItem.rightBarButtonItem.tintColor = [UIColor whiteColor];
5.設(shè)置標(biāo)題
self.navigationItem.title =@"標(biāo)題";
self.navigationBar.titleTextAttributes = @{NSFontAttributeName:[UIFontsystemFontOfSize:16],NSForegroundColorAttributeName:[UIColor whiteColor]};
6.標(biāo)題位置設(shè)置成view
UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 44)];
view.backgroundColor = [UIColor yellowColor];
self.navigationItem.titleView = view;
7.Next按鈕響應(yīng)第二個(gè)界面
-(void)gotoSecondView{
SecondViewController * svc = [[SecondViewController alloc]init];
//設(shè)置默認(rèn)提供的返回按鈕的標(biāo)題
UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStyleDone target:nil action:nil];
self.navigationItem.backBarButtonItem = backItem;
//present:在現(xiàn)有的界面上蓋上一層 dismissViewController來(lái)消除
//[self.navigationController presentViewController:svc animated:YES completion:nil];
//push:push入棧 pop出棧來(lái)消除
[self.navigationController pushViewController:svc animated:YES];
}