andrewlundy andrewlundy - 1 month ago 11
iOS Question

iOS navigationBar not changing color when I set new font

I'm working on a simple church app. And I've started the slide-out menu and I can't seem to change the color of the font of the navigationBar when I set it to a new font. The colors will change when I don't have the line of code written for the font change. But, as soon as I add the line of code for the font change, the color goes back to the default. I've provided my code below, and pictures to go along with them. I've tried putting this function in the AppDelegate (I saw multiple forums saying this was the trick), but nothing seems to be working.

When I do this, the color is changed.

func customizeNavBar() {

navigationController?.navigationBar.tintColor = UIColor(colorLiteralRed: 255/255, green: 255/255, blue: 255/255, alpha: 1)
navigationController?.navigationBar.barTintColor = UIColor(colorLiteralRed: 0/255, green: 0/255, blue: 0/255, alpha: 1)


navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]

}


enter image description here

When I add the NSFontAttributeName line of code, the font type changes, but the color goes back to black.

func customizeNavBar() {

navigationController?.navigationBar.tintColor = UIColor(colorLiteralRed: 255/255, green: 255/255, blue: 255/255, alpha: 1)
navigationController?.navigationBar.barTintColor = UIColor(colorLiteralRed: 0/255, green: 0/255, blue: 0/255, alpha: 1)


navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
navigationController?.navigationBar.titleTextAttributes = [NSFontAttributeName: UIFont(name: "AvenirNext-Regular", size: 21)!]

}


enter image description here

Answer Source

You've replaced your foreground color with the second attribute. You need to combine them

Try this

navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white,
NSFontAttributeName: UIFont(name: "AvenirNext-Regular", size: 21)!]