Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwiftUI update navigation bar title color

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 ?

like image 952
Prashant Tukadiya Avatar asked Jun 08 '19 10:06

Prashant Tukadiya


People also ask

How do I change the navigation title text color in Swift?

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.

How do I customize the navigation bar title in SwiftUI?

To customize a navigation bar title view in SwiftUI, we simply set ToolbarItem of placement type . principal to a new . toolbar modifier.


2 Answers

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())     } }  

Modified navigation bar

like image 126
arsenius Avatar answered Sep 18 '22 15:09

arsenius


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!!

like image 26
Anjali Kevadiya Avatar answered Sep 20 '22 15:09

Anjali Kevadiya