How to change navigation title color in swiftUI

Hi, There


I am learning SwiftUI, I want change navigation Title Color. I have set navigation Title using .navigationTitle("Parent Login")


I have tried to color of navigation title using below code.


struct ContentView: View {


    var body: some View {


        NavigationView {


            Text("Hello, World!")


                .navigationBarTitle("My Title", displayMode: .inline)


                .navigationBarItems(trailing: Text("Trailing"))


                .navigationBarTitleColor(.red) // Set the navigation title color here


        }


    }


}




extension View {


    func navigationBarTitleColor(_ color: Color) -> some View {


        return self.modifier(NavigationBarTitleColorModifier(color: color))


    }


}




struct NavigationBarTitleColorModifier: ViewModifier {


    var color: Color


    


    func body(content: Content) -> some View {


        content


            .onAppear() {


                let coloredAppearance = UINavigationBarAppearance()


                coloredAppearance.titleTextAttributes = [.foregroundColor: UIColor(color)]


                coloredAppearance.largeTitleTextAttributes = [.foregroundColor: UIColor(color)]


                UINavigationBar.appearance().standardAppearance = coloredAppearance


                UINavigationBar.appearance().scrollEdgeAppearance = coloredAppearance


            }


    }


}


I have checked apple's document but did not get proper demo or answer to change color.


Please comment if anyone has idea about it


Apple also deprecated .navigationBarTitle(<#T##title: Text##Text#>) method which takes Text styled view so we can change navigation title color.

MacBook Pro 16″, macOS 11.2

Posted on Dec 14, 2023 10:00 PM

Reply
3 replies

Feb 18, 2024 9:08 AM in response to 3s_com

To change the navigation bar title color you just have to add a modifier. Then call that modifier with the chosen color.


extension View {
    @available(iOS 14, *)
    func navigationBarTitleTextColor(_ color: Color) -> some View {
        let uiColor = UIColor(color)
        UINavigationBar.appearance().titleTextAttributes = [.foregroundColor: uiColor ]
        UINavigationBar.appearance().largeTitleTextAttributes = [.foregroundColor: uiColor ]
        return self
    }
}


You can insert this piece of code wherever you want in your project files. Simply call .navigationBarTitleTextColor(.color) where you've set your navigation bar title.

Dec 22, 2023 3:43 PM in response to 3s_com

In SwiftUI, changing the navigation bar title color involves using a combination of SwiftUI views and UIKit components due to some limitations in SwiftUI's current state.


Here's a revised version of your code:


import SwiftUI

struct ContentView: View {
    var body: some View {
        NavigationView {
            Text("Hello, World!")
                .navigationBarTitle("My Title", displayMode: .inline)
                .navigationBarItems(trailing: Text("Trailing"))
                .navigationBarColor(.red) // static property of `Color` struct
        }
    }
}

extension View {
    func navigationBarColor(_ color: UIColor) -> some View {
        self.modifier(NavigationBarColorModifier(color: color))
    }
}

struct NavigationBarColorModifier: ViewModifier {
    var color: UIColor

    func body(content: Content) -> some View {
        content
            .onAppear {
                let coloredAppearance = UINavigationBarAppearance()
                coloredAppearance.configureWithOpaqueBackground()
                coloredAppearance.titleTextAttributes = [.foregroundColor: color]
                coloredAppearance.largeTitleTextAttributes = [.foregroundColor: color]

                UINavigationBar.appearance().standardAppearance = coloredAppearance
                UINavigationBar.appearance().compactAppearance = coloredAppearance
                UINavigationBar.appearance().scrollEdgeAppearance = coloredAppearance
            }
    }
}



This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

How to change navigation title color in swiftUI

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.