导航控制器是iOS中常用的多视图控制器之一,它用来管理多个视图控制器
导航控制器所控制的视图控制器都有一定的层级关系
UINavigationController继承于UIViewController,以栈的方式管理所控制的视图控制器,至少要有一个被管理的视图控制器。
创建的时候需要用户提供一个视图控制器作为导航控制器的一个根视图控制器。
创建导航控制器 // 1.创建自定义的viewController
RootViewController*rootVC = [[RootViewControlleralloc]init];
// 2.创建系统的navigationController,由自定义的viewController进行初始化
UINavigationController*rootNVC = [[UINavigationControlleralloc]initWithRootViewController:rootVC];
// 3.将窗口的根视图控制器设置为创建出的navigationController
self.window.rootViewController= rootNVC;
[rootNVCrelease];
[rootVCrelease];
导航
UINavigationController通过控制入栈和出栈来展示各个视图控制器.(先进后出,后进先出)
//进入下一个视图控制器
- ( void )pushViewController:( UIViewController *)viewController animated:( BOOL )animated; // Uses a horizontal slide transition. Has no effect if the view controller is already in the stack.//返回上一个视图控制器 - ( UIViewController *)popViewControllerAnimated:( BOOL )animated; // Returns the popped controller.
//返回到指定的视图控制器
- (NSArray*)popToViewController:(UIViewController*)viewController animated:(BOOL)animated;// Pops view controllers until the one specified is on top. Returns the popped controllers.
//返回到根视图控制器
- (NSArray*)popToRootViewControllerAnimated:(BOOL)animated;// Pops until there's only a single view controller left on the stack. Returns the popped controllers.
2. 定制UINavigationBar
如果将navigationBar的透明度关闭之后,navigationBar的高度将会变为44。(navigationBar的高度一直是44)
1)修改UINavigationBar的背景图片
//320 * 44 或者 320 * 64(navigationBar变成黑色) navigationBar的背景图片格式要求
//设置背景图片
[self.navigationController.navigationBarsetBackgroundImage:[UIImageimageNamed:@"NavBar_64.png"]forBarMetrics:UIBarMetricsDefault];
2)修改UINavigationBar是否透明//将navigationbar设置为不透明 这样屏幕左上角就不是(0,0)了,而是其下边缘
self . navigationController . navigationBar . translucent = NO ;
3)修改当前的Bar的背景颜色
self . navigationController . navigationBar . barTintColor = [ UIColor blackColor ];
-修改bar的颜色如果为黑其中的字都为白色,如果为默认那么字为黑色
4)修改Bar上的标题title
//判断是否有Bar 有的话要显示在Bar上,没有就不会显示
self . title = @"首页” ;
// self.navigationItem.title = @"首页”;
设置导航栏上的视图
1) 设置左右按钮 可以设置方法
//创建barButtonItem
UIBarButtonItem *leftBarButton = [[UIBarButtonItemalloc]initWithBarButtonSystemItem:UIBarButtonSystemItemPlaytarget:selfaction:@selector(buttonAction)];
self.navigationItem.leftBarButtonItem = leftBarButton;
[leftBarButton release];
UIBarButtonItem *rightBarButton = [[UIBarButtonItemalloc]initWithBarButtonSystemItem:UIBarButtonSystemItemPausetarget:selfaction:@selector(buttonAction)];
self.navigationItem.rightBarButtonItem = rightBarButton;
[rightBarButton release];
2) 有多个右(左)侧按钮,放在一个数组里面UIBarButtonItem*leftBarButton = [[UIBarButtonItemalloc]initWithBarButtonSystemItem:UIBarButtonSystemItemPlaytarget:selfaction:@selector(buttonAction)];
UIBarButtonItem *rightBarButton = [[UIBarButtonItemalloc]initWithBarButtonSystemItem:UIBarButtonSystemItemPausetarget:selfaction:@selector(buttonAction)];
NSArray *buttonItems = @[ leftBarButton,rightBarButton ] ;
//左侧按钮
self.navigationItem.leftBarButtonItems = buttonItems;
//右侧按钮
self.navigationItem.rightBarButtonItems = buttonItems;
//工具栏self . navigationController . toolbar . items = buttonItems;
[leftBarButton release ];
[rightBarButton release ];
//关联方法
-( void )buttonAction { DetailViewController *detailVC = [[ DetailViewController alloc ] init ]; [ self . navigationController pushViewController :detailVC animated : YES ]; }