FirstViewController *firstVC=[[FirstViewController alloc]initWithNibName:@"FirstViewController" bundle:Nil];
//实例化导航控制器
UINavigationController *navController=[[UINavigationController alloc]initWithRootViewController:firstVC];
//将包装好的导航控制器,作为rootviewcontroller
self.window.rootViewController=navController;
以上是UINavigationController实例化
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
使用UINavigationController推出下一个页面:
[self.navigationController pushViewController:secondVC animated:YES];
UINavigationController各种属性设置
if (ISiOS7) {
//在IOS7里,这个是设置导航条文字的颜色
self.navigationController.navigationBar.tintColor=[UIColor redColor];
//在IOS7里,这个是设置导航条背景的颜色
self.navigationController.navigationBar.barTintColor=[UIColor blueColor];
}else{
//不是ios7,只能设置导航条的颜色
self.navigationController.navigationBar.tintColor=[UIColor orangeColor];
}
//自定义标题
UILabel *titleLable=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 44)];
//ios6里,label默认背景是白色,但是ios7默认透明
if (!ISiOS7) {
titleLable.backgroundColor=[UIColor clearColor];
}
titleLable.text=self.title;
titleLable.font=[UIFont boldSystemFontOfSize:20];
titleLable.textColor=[UIColor whiteColor];
self.navigationItem.titleView=titleLable;
//自定义导航条按键(利用系统自带的样式)
UIBarButtonItem *barButtonItem1=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(onBarButtonItemClicked:)];
UIBarButtonItem *barButtonItem2=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(onBarButtonItemClicked:)];
// self.navigationItem.rightBarButtonItem=barButtonItem;//单独一个按键
self.navigationItem.rightBarButtonItems=@[barButtonItem1,barButtonItem2];
//自定义导航条按键,完全自定义
UIButton *leftBarButton=[UIButton buttonWithType:UIButtonTypeCustom];
[leftBarButton setTitle:@"左侧按键" forState:UIControlStateNormal];
[leftBarButton setTitle:@"左侧点击" forState:UIControlStateHighlighted];
[leftBarButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[leftBarButton setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
leftBarButton.frame=CGRectMake(0, 0, 100, 44);
[leftBarButton addTarget:self action:@selector(onLeftBarButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
//将上面自定义的按键,包装成一个导航条按键
UIBarButtonItem *leftBarButtonItem=[[UIBarButtonItem alloc]initWithCustomView:leftBarButton];
self.navigationItem.leftBarButtonItem=leftBarButtonItem;
//设置导航条背景
[self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navbg"] forBarMetrics:UIBarMetricsDefault];