Search code examples
iosswiftuinavigationbarappdelegateappearance

UINavigationBar won't set title text attributes


Im trying to set my apps title text color to white, however when i try setting the titleTextAttributes nothing appears to work.

I've got other appearance properties set such as background color and tint colour. But the NSAttributedString.Key.foregroundColor appears to not work correctly. I've double check documentation to ensure its taking UIColor and still appears to be set up correctly with no results. Below is my AppDelegate for reference.

//Set the window as the visible view.
window = UIWindow(frame: UIScreen.main.bounds)
window?.makeKeyAndVisible()


// Make the view controller default the homeController.
window?.rootViewController = UINavigationController(rootViewController: HomeController())

//Customise navBar appearance   
let navBarAppearance = UINavigationBar.appearance()
navBarAppearance.isTranslucent = false
navBarAppearance.barTintColor = .backgroundLightBlack
navBarAppearance.tintColor = .seaFoamBlue

//This is the line that doesnt work :( 
navBarAppearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]

Solution

  • Found the answer! In case anyone comes across this.

    I was using the prefersLargeTitle = true navigation bar.

    In order to set the title text attributes for this style, you need the LargeTitleTextAttributes which is set exactly the same as a regular title attribute but with the largeTitleTextAttributes property instead e.g:

    navBarAppearance.largeTitleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]