导航控制器:视图控制器的管理器创建导航控制器必须存在一个跟控制器
在APPdelegate如下:
FirstViewController * vc= [[FirstViewController alloc] init];
//实例化导航控制器,创建时必须加载主控制器
//导航控制器是栈结构,先进后出
UINavigationController * nav = [[UINavigationController alloc]initWithRootViewController:vc];
self.window.rootViewController = nav;
通过导航实现跳转如下:
SecondViewController * secondVC = [[SecondViewController alloc] init];
//导航控制器添加子控制器
[self.navigationController
pushViewController:secondVC animated:YES]]跳转;
[self.navigationController
popViewControllerAnimated:YES];返回
[self.navigationController popToRootViewControllerAnimated:YES];返回首页(根视图)
导航控制器相应的属性
UIBarButtonItem设置导航的按钮
UIBarButtonItem * right=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:nil action:nil];
//设置当前页面的右2按钮
self.navigationItem.rightBarButtonItems= @[leftItem,leftItem1];
先设置一个按钮,通过下面方法设置导航按钮:
UIBarButtonItem * rightItem1 = [[UIBarButtonItem alloc]
initWithCustomView:button(按钮名字)];
按钮是一个图片如下
UIImage * image = [UIImage imageNamed:@"setting_item"];
//image的渲染模式(使图片保持原样防止改色)
image = [imageimageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIBarButtonItem * rightItem2 =[[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlaintarget:nil action:nil];
修改所有按钮颜色
self.navigationController.navigationBar.tintColor =[UIColor greenColor];
//修改导航条颜色,当修改bar颜色时,会丢失毛玻璃效果self.navigationController.navigationBar.barTintColor = [UIColororangeColor];
//bar风格
self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
//修改bar的透明度self.navigationController.navigationBar.translucent = NO;
//修改题目样式
//NSDictionary * dict1 = @{NSFontAttributeName:[UIFontsystemFontOfSize:30],
//NSForegroundColorAttributeName:[UIColorpurpleColor]};
//[self.navigationController.navigationBar setTitleTextAttributes:dict1];
//修改题目类型
//UISegmentedControl * seg = [[UISegmentedControl alloc]initWithItems:@[@"消息",@"电话"]];
//seg.frame= CGRectMake(0, 0, 100, 30);
//seg.selectedSegmentIndex = 0;
//self.navigationItem.titleView = seg;
//修改item的样式
NSDictionary * dict2 =@{NSForegroundColorAttributeName:[UIColorgrayColor],NSFontAttributeName:[UIFont systemFontOfSize:20]};
[self.navigationItem.rightBarButtonItemsetTitleTextAttributes:dict2 forState:UIControlStateNormal];
在导航条上添加一块内容
//self.navigationItem.prompt = @"qq通话中";
//设置bar背景图片
//[self.navigationController.navigationBarsetBackgroundImage:[UIImage imageNamed:@"barnav"]forBarMetrics:UIBarMetricsDefault];
//设置导航条的隐藏
//self.navigationController.navigationBarHidden= NO;
设置返回按钮
UIBarButtonItem * backItem =[[UIBarButtonItem alloc] initWithTitle:@"返回"style:UIBarButtonItemStylePlain target:nil action:nil];
//此处不好
//UIBarButtonItem * backItem =[[UIBarButtonItem alloc] initWithImage:[UIImageimageNamed:@"backBtn"] style:UIBarButtonItemStylePlain target:nilaction:nil];
//backItem.title = @"返回";
//self.navigationItem.backBarButtonItem =backItem;
//[self.navigationController.navigationBarsetBackIndicatorImage:[UIImage imageNamed:@"backBtn"]];
//[self.navigationController.navigationBarsetBackIndicatorTransitionMaskImage:[UIImage imageNamed:@"backBtn"]];
封装自定义导航条见自定义导航条。
//可以修改程序中所有的UINavigationBar以及bar.的属性。
UINavigationBar * bar = [UINa
vigationBar appearance];