Change navigation bar tittle text to custom color?

Viewed 69

I am trying to change the title bar text to a custom shade of lavender with the following code in my appDelegate

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor(red: 0.9069682956, green: 0.7839415669, blue: 0.9612388015, alpha: 1)]

    return true
}

However the title bar text is showing up black still. Is there a better way to change this?

1 Answers

Probably it is too early, try to add in init of ContentView and, as well, add some for large title (which is default for SwiftUI NavigationView), like

struct ContentView: View {

    init() {
        UINavigationBar.appearance().titleTextAttributes = 
            [NSAttributedString.Key.foregroundColor: 
               UIColor(red: 0.9069682956, green: 0.7839415669, blue: 0.9612388015, alpha: 1)]
        UINavigationBar.appearance().largeTitleTextAttributes = 
            [NSAttributedString.Key.foregroundColor: 
               UIColor(red: 0.9069682956, green: 0.7839415669, blue: 0.9612388015, alpha: 1)]
    }

    // ... other code with NavigationView here
}

Tested with Xcode 12.1 / iOS 14.1

Related