I think all day to change the navigation Bar title color, but it doesn't work. this is my code:
var user: User? { didSet { navigationItem.title = user?.name observeMessages() } }
I use didSet to show the title on the navigation title.
The text color of the navigation bar can be changed using two inbuilt classes: navbar-light: This class will set the color of the text to dark. This is used when using a light background color. navbar-dark: This class will set the color of the text to light.
Start with Navigation ControllerCreate a single view application in Xcode. Add two view controller into your storyboard. Create two different swift files for those view controllers and set identifiers for them. Take a button in each view controller, set constrain for them and customize as you want.
Add this in your code . .
let textAttributes = [NSForegroundColorAttributeName:UIColor.red] navigationController?.navigationBar.titleTextAttributes = textAttributes
SWIFT 4:
let textAttributes = [NSAttributedStringKey.foregroundColor:UIColor.red] navigationController?.navigationBar.titleTextAttributes = textAttributes
SWIFT 4.2+:
let textAttributes = [NSAttributedString.Key.foregroundColor:UIColor.red] navigationController?.navigationBar.titleTextAttributes = textAttributes
Keeping all the other attributes of the title: If you just want change the color you could do like this:
if var textAttributes = navigationController?.navigationBar.titleTextAttributes { textAttributes[NSAttributedString.Key.foregroundColor] = UIColor.red navigationController?.navigationBar.titleTextAttributes = textAttributes }
The title color of Navigation Bar can be changed in Storyboard.
Go to Attributes inspector of Navigation Controller > Navigation Bar and set the desired color in Title Color menu.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With