How to change the navigation bar title color in SwiftUI
NavigationView { List{ ForEach(0..<15) { item in HStack { Text("Apple") .font(.headline) .fontWeight(.medium) .color(.orange) .lineLimit(1) .multilineTextAlignment(.center) .padding(.leading) .frame(width: 125, height: nil) Text("Apple Infinite Loop. Address: One Infinite Loop Cupertino, CA 95014 (408) 606-5775 ") .font(.subheadline) .fontWeight(.regular) .multilineTextAlignment(.leading) .lineLimit(nil) } } } .navigationBarTitle(Text("TEST")).navigationBarHidden(false).foregroundColor(.orange) }
I have tried with .foregroundColor(.orange)
but it is not working
also tried .navigationBarTitle(Text("TEST").color(.orange))
Any help ?
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.
To customize a navigation bar title view in SwiftUI, we simply set ToolbarItem of placement type . principal to a new . toolbar modifier.
It is not necessary to use .appearance()
to do this globally.
Although SwiftUI does not expose navigation styling directly, you can work around that by using UIViewControllerRepresentable
. Since SwiftUI is using a regular UINavigationController
behind the scenes, the view controller will still have a valid .navigationController
property.
struct NavigationConfigurator: UIViewControllerRepresentable { var configure: (UINavigationController) -> Void = { _ in } func makeUIViewController(context: UIViewControllerRepresentableContext<NavigationConfigurator>) -> UIViewController { UIViewController() } func updateUIViewController(_ uiViewController: UIViewController, context: UIViewControllerRepresentableContext<NavigationConfigurator>) { if let nc = uiViewController.navigationController { self.configure(nc) } } }
And to use it
struct ContentView: View { var body: some View { NavigationView { ScrollView { Text("Don't use .appearance()!") } .navigationBarTitle("Try it!", displayMode: .inline) .background(NavigationConfigurator { nc in nc.navigationBar.barTintColor = .blue nc.navigationBar.titleTextAttributes = [.foregroundColor : UIColor.white] }) } .navigationViewStyle(StackNavigationViewStyle()) } }
In SwiftUI, you can not change the navigationTitleColor directly. You have to change UINavigation's appearance in init()
like this,
struct YourView: View { init() { //Use this if NavigationBarTitle is with Large Font UINavigationBar.appearance().largeTitleTextAttributes = [.foregroundColor: UIColor.red] //Use this if NavigationBarTitle is with displayMode = .inline UINavigationBar.appearance().titleTextAttributes = [.foregroundColor: UIColor.red] } var body: some View { NavigationView { List{ ForEach(0..<15) { item in HStack { Text("Apple") .font(.headline) .fontWeight(.medium) .color(.orange) .lineLimit(1) .multilineTextAlignment(.center) .padding(.leading) .frame(width: 125, height: nil) Text("Apple Infinite Loop. Address: One Infinite Loop Cupertino, CA 95014 (408) 606-5775 ") .font(.subheadline) .fontWeight(.regular) .multilineTextAlignment(.leading) .lineLimit(nil) } } } .navigationBarTitle(Text("TEST")).navigationBarHidden(false) //.navigationBarTitle (Text("TEST"), displayMode: .inline) } } }
I hope it will work. Thanks!!
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