[navigationController.navigationBar setBarStyle:UIBarStyleBlack];
Customizing navigationBar in iOS7 - title color not working
-
30-06-2022 - |
题
I am trying to customize the navigationBar of a navigation controller in iOS7 and the title color is not changing. I am doing the following:
[navigationController.navigationBar setBarTintColor:[UIColor colorWithRed:46.0f/256.0f green:46.0f/256.0f blue:46.0f/256.0f alpha:1]];
[navigationController.navigationBar setTranslucent:NO];
[navigationController.navigationBar setTitleTextAttributes:@{[UIColor whiteColor]:UITextAttributeTextColor}];
[self presentViewController:navigationController animated:YES completion:nil];
The navigationBar translucency gets turned off and it is dark, but the title also stays dark. I've also tried to create a custom label and set it as the title view with not much luck.
How can the title color be changed?
解决方案 3
其他提示
The appearance api continues to evolve, and UITextAttributeTextColor is now replaced with NSForegroundColorAttributeName.
[navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];
I'll add two things:
The key comes first, then the object.
If you want to globally change your nav controller's title attributes, use the appearance api:
[[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor whiteColor]}];
I'd put that appearance api call in your app delegate's didFinishLaunchingWithOptions method.
UPDATE: Might as well post the Swift equivalents.
To update the navigationBar for an individual view controller, one can use:
self.navigationController.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.whiteColor()]
To change the navigation bar's appearance throughout an entire app, one can use:
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName:UIColor.whiteColor()]
Not working for me. Using UINavigation as a modal view controller.
Apparently title colour needs to be set uiapplication level
Edit:
Best way is to change the navigation title in each VC context:
[self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], NSForegroundColorAttributeName,nil]];
In swift you'll do the following:
self.navigationController.navigationBar.titleTextAttributes = [
NSForegroundColorAttributeName : UIColor.redColor(),
NSFontAttributeName : UIFont.systemFontOfSize(20)
]
Swift + UIAppearance version:
UINavigationBar.appearance().barTintColor = .blackColor()
UINavigationBar.appearance().barStyle = .Black
The problem in the OP's code is this line:
[navigationController.navigationBar setTitleTextAttributes:@{[UIColor whiteColor]:UITextAttributeTextColor}];
The text color attribute name and value are mixed up. It should be
[navigationController.navigationBar setTitleTextAttributes:@{UITextAttributeTextColor:[UIColor whiteColor]}];
actually looks like a bug. Try
myViewcontroller.title = @"";
navigationController.navigationBar.barStyle = UIBarStyleBlack;
myViewcontroller.title = @"TITLE";
works.
In Swift 4
NSForegroundColorAttributeName was renamed to NSAttributedString.Key.foregroundColor
So If You're looking to change the navigationItem title's color:
let navBarAppearance = self.navigationController!.navigationBar
navBarAppearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor.white]
I used UIColor.white, just insert any UIColor you want the title to be.