UINavigationController是IOS編程中比較常用的一種容器view controller,也就是導航欄控制器。
1.創建一個UINavigationController
self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
FirstViewController * first = [[FirstViewController alloc]init];
UINavigationController * nav = [[UINavigationControlleralloc]initWithRootViewController:first]; //UINavigationController第一個顯示的界面
self.window.rootViewController = nav;
[self.window makeKeyAndVisible];
2.隱藏導航欄工具欄
self.navigationController.navigationBarHidden = YES;
self.navigationController.toolbarHidden = NO;
3.設置導航欄和工具欄的顏色(設置了之后所有都是這個顏色,因為只有一個navigationController)
self.navigationController.toolbar.barTintColor = [UIColor redColor];
self.navigationController.navigationBar.barTintColor = [UIColor redColor];
4.設置左右顯示的按鈕
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.設置標題
self.navigationItem.title =@"標題";
self.navigationBar.titleTextAttributes = @{NSFontAttributeName:[UIFontsystemFontOfSize:16],NSForegroundColorAttributeName:[UIColor whiteColor]};
6.標題位置設置成view
UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 44)];
view.backgroundColor = [UIColor yellowColor];
self.navigationItem.titleView = view;
7.Next按鈕響應第二個界面
-(void)gotoSecondView{
SecondViewController * svc = [[SecondViewController alloc]init];
//設置默認提供的返回按鈕的標題
UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStyleDone target:nil action:nil];
self.navigationItem.backBarButtonItem = backItem;
//present:在現有的界面上蓋上一層 dismissViewController來消除
//[self.navigationController presentViewController:svc animated:YES completion:nil];
//push:push入棧 pop出棧來消除
[self.navigationController pushViewController:svc animated:YES];
}