基础设置
- 隐藏导航栏
self.navigationController.navigationBarHidden = YES;
- 去掉透明效果
[self.navigationController.navigationBar setTranslucent:NO];
提示:
NO:无透明效果 + 会向下偏移64
YES:有透明效果 + 不会向下偏移64
中间标题
- 修改中间标题的文字颜色
self.navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObject:[UIColor colorWithRed:1.0 green:0.4118 blue:0.4392 alpha:1.0] forKey:UITextAttributeTextColor];
- 单独设置nav标题
NSDictionary *navTitleArr = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont boldSystemFontOfSize:20],UITextAttributeFont,[UIColor redColor],UITextAttributeTextColor ,[NSValue valueWithCGSize:CGSizeMake(2.0, 2.0)] , UITextAttributeTextShadowOffset ,[UIColor whiteColor] ,UITextAttributeTextShadowColor ,nil];
[self.navigationController.navigationBar setTitleTextAttributes:navTitleArr];
左右item设置
- 改变返回按钮的颜色
self.navigationController.navigationBar.tintColor = [UIColor redColor];
- 隐藏返回按钮
[self.navigationItem setHidesBackButton:YES];
- 去掉返回按钮的文字(如果添加了左按钮,可以自动去掉返回按钮的文字)
[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60) forBarMetrics:UIBarMetricsDefault];
- 添加右按钮
UIBarButtonItem *leftBtnItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"111"] style:UIBarButtonItemStyleDone target:self action:@selector(share:)];
[self.navigationItem setRightBarButtonItem:leftBtnItem];
- 可以把左按钮 - 做成返回按钮
UIBarButtonItem *leftBtnItem= [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"nav_leftbtn"] style:UIBarButtonItemStyleDone target:self action:@selector(goBackAction)];
leftBtnItem.imageInsets = UIEdgeInsetsMake(0, -10, 0, 0);
[self.navigationItem setLeftBarButtonItem:leftBtnItem];
- 修改返回按钮背景
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(0, 0, 25, 25);
UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithCustomView:btn];
[btn setImage:[UIImage imageNamed:@"nav_leftbtn"] forState:UIControlStateNormal];
[btn addTarget:self action:@selector(goBackAction:) forControlEvents:UIControlEventTouchUpInside];
btn.imageEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 10);
[self.navigationItem setLeftBarButtonItem:leftItem];
Nav背景
- 修改Nav背景
(1)半透明
self.navigationController.navigationBar.backgroundColor = [UIColor colorWithRed:0.98f green:0.98f blue:0.98f alpha:1.00f];
(2)不带半透明的效果
self.navigationController.navigationBar.barTintColor = [UIColor redColor];
- 更改nav背景图片
UIImage *image = [UIImage imageNamed:@"333"];
[self.navigationController.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
- 去掉nav 那个黑色的分割线
[self.navigationController.navigationBar setShadowImage:[UIImage new]];
其他
- 添加系统默认的nav手势返回
self.navigationController.interactivePopGestureRecognizer.enabled = YES;
self.navigationController.interactivePopGestureRecognizer.delegate=(id)self;