- // 1 设置导航栏标题
- // 方法1
- self.title = @"导航栏控制器";
- // 方法2 当有tabbarController时使用该方法,避免影响标题设置
- self.navigationItem.title = @"导航栏控制器标题";
- // 2 设置自定义导航栏标题子视图
- UIImageView *titleImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, 40.0, 40.0)];
- titleImageView.image = [UIImage imageNamed:@"image"];
- self.navigationItem.titleView = titleImageView;
- // 3 设置导航栏控制器-右按钮
- // 3-1 设置单个按钮
- // self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"next" style:UIBarButtonItemStyleDone target:self action:@selector(nextClick:)];
- // 3-2 设置多个按钮
- UIBarButtonItem *playItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemPlay target:self action:@selector(nextClick:)];
- UIBarButtonItem *stopItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemStop target:self action:@selector(doneClick:)];
- NSArray *itemsRight = [NSArray arrayWithObjects:playItem, stopItem, nil nil];
- self.navigationItem.rightBarButtonItems = itemsRight;
- - (void)nextClick:(UIBarButtonItem *)barButton
- {
- NSLog(@"你点击了 next ");
- }
- - (void)doneClick:(UIBarButtonItem *)barButton
- {
- NSLog(@"你点击了 done ");
- }
- // 4 设置导航栏控制器-左按钮
- // 4-1 设置单个按钮
- // self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:@selector(sureClick:)];
- // 3-2 设置多个按钮
- UIBarButtonItem *sureItem = [[UIBarButtonItem alloc] initWithTitle:@"确定" style:UIBarButtonItemStyleDone target:self action:@selector(sureClick:)];
- UIBarButtonItem *cancelItem = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStyleDone target:self action:@selector(cancelClick:)];
- NSArray *itemsLeft = [NSArray arrayWithObjects:sureItem, cancelItem, nil nil];
- self.navigationItem.leftBarButtonItems = itemsLeft;
- - (void)sureClick:(UIBarButtonItem *)barButton
- {
- NSLog(@"你点击了 sure ");
- }
- - (void)cancelClick:(UIBarButtonItem *)barButton
- {
- NSLog(@"你点击了 cancel ");
- }
- // 5 设置导航栏控制器显示样式
- // 样式
- self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
- // 导航按钮颜色
- self.navigationController.navigationBar.tintColor = [UIColor greenColor];
- // 背景颜色
- self.navigationController.navigationBar.barTintColor = [UIColor yellowColor];
- // 背景图片
- [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"image"] forBarMetrics:UIBarMetricsDefault];
- // 背景阴影
- self.navigationController.navigationBar.shadowImage = [UIImage imageNamed:@"image"];
- // 字体标题设置(字体大小、字体颜色设置)
- [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12.0], NSForegroundColorAttributeName:[UIColor yellowColor]}];
- // 6 导航栏控制器的显示,或隐藏
- UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(10.0, 80.0, (CGRectGetWidth(self.view.bounds) - 10.0 * 2), 30.0)];
- [self.view addSubview:button];
- button.backgroundColor = [UIColor redColor];
- [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
- [button setTitleColor:[UIColor yellowColor] forState:UIControlStateHighlighted];
- [button setTitle:@"隐藏导航栏" forState:UIControlStateNormal];
- [button setTitle:@"显示导航栏" forState:UIControlStateSelected];
- [button addTarget:self action:@selector(hiddenClick:) forControlEvents:UIControlEventTouchUpInside];
- - (void)hiddenClick:(UIButton *)button
- {
- button.selected = !button.selected;
- if (button.selected)
- {
- // self.navigationController.navigationBarHidden = YES; // 方法1
- [self.navigationController setNavigationBarHidden:YES animated:YES]; // 方法2 常用方法,特别是视图控制器之间跳转时避免产生黑色区域
- }
- else
- {
- // self.navigationController.navigationBarHidden = NO;
- [self.navigationController setNavigationBarHidden:NO animated:YES];
- }
- }