释放双眼,带上耳机,听听看~!
1、透明导航栏+定制title颜色
// 重写需要使用透明导航栏的VC的viewWillAppear方法
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.shadowImage = [UIImage new];
self.navigationController.navigationBar.translucent = YES;
self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor]}; // title颜色
}
2、首页透明导航栏,push进一个默认导航栏的页面
// 重写需要使用默认导航栏的VC的viewWillAppear方法
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.shadowImage = nil;// 要使用默认导航栏页面的话,需要设置为nil,否则没有导航栏下面的那根线
self.navigationController.navigationBar.translucent = NO;
}
// 既然在首页的viewWillAppear写了透明的代码,为什么还要在这里写呢?我在尝试的时候发现,不这样的话,透明的效果会晚一点生效,看起来不自然
- (void)willMoveToParentViewController:(UIViewController *)parent{
if (!parent){
self.navigationController.navigationBar.translucent = YES;
self.navigationController.navigationBar.shadowImage = [UIImage new];
}
}

不重写willMoveToParentViewController的样子:点击test,然后点返回按钮,导航栏的透明效果生效的不及时,如果使用滑动返回手势,透明效果生效及时

改后的效果
3、一个坑:
A页导航栏文字白色,push进B页面,颜色使用黑色,然后点击返回按钮返回,导航栏文字设为白色无效!!!首页的viewWillAppear执行了,但是不起作用。(使用滑动返回手势,是起作用的)
// A页
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor]}; // title颜色
}
// B页
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor blackColor]}; // title颜色
}
解决方法同2,要在B页里重写方法(同2中改后的效果):
- (void)willMoveToParentViewController:(UIViewController *)parent{
if (!parent){
self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor]}; // title颜色
}
}
4、A页Push进B页,去掉返回按钮中的文字,仅保留箭头,并且定制箭头颜色
// A页
- (void)gotoB{
[self performSegueWithIdentifier:@"gotoB" sender:nil];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc]init];
backButton.title = @"";
self.navigationItem.backBarButtonItem = backButton;
}
// B页
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:228.0/255 green:156.0/255 blue:0 alpha:1];
}