0

I tried this but not working :

    init() {
        let navBarAppearance = UINavigationBar.appearance()
        navBarAppearance.largeTitleTextAttributes = [.foregroundColor: UIColor.white]
        navBarAppearance.titleTextAttributes = [.foregroundColor: UIColor.white]
    }

Ken White
  • 123,280
  • 14
  • 225
  • 444
Flincorp
  • 751
  • 9
  • 22

2 Answers2

1

Try the following code:

init(){
    //set nav bar style
    let coloredAppearance = UINavigationBarAppearance()
    
    coloredAppearance.configureWithTransparentBackground()
    //set background color
    coloredAppearance.backgroundColor = UIColor.red
    coloredAppearance.shadowColor = UIColor.lightGray
    //set title color
    coloredAppearance.titleTextAttributes = [.foregroundColor: UIColor.white]
    coloredAppearance.largeTitleTextAttributes = [.foregroundColor: UIColor.white] 
    
    //make the appearance work
    UINavigationBar.appearance().standardAppearance = coloredAppearance
    UINavigationBar.appearance().compactAppearance = coloredAppearance
    UINavigationBar.appearance().scrollEdgeAppearance = coloredAppearance
    UINavigationBar.appearance().tintColor = UIColor.white
    
    //set backbutton and its color
    let image = UIImage(systemName: "chevron.backward")?.withTintColor(UIColor.white, renderingMode: .alwaysOriginal)
    coloredAppearance.setBackIndicatorImage(image, transitionMaskImage: image)
}

Output: enter image description here

JUsltop
  • 44
  • 2
  • It works but as soon as i use : .toolbarBackground(Color.blue, for: .navigationBar) .toolbarBackground(.visible, for: .navigationBar) the navigationTitle revert to black, why? – Flincorp Nov 09 '22 at 12:22
-1

You can use .toolbar modifier as shown below:

struct ContentView: View {
    var body: some View {
        NavigationStack {
            VStack {
                
            }
            .navigationBarTitleDisplayMode(.large)
            .toolbar {
                ToolbarItem(placement: .principal) {
                    Text("Accounts")
                        .frame(maxWidth: .infinity, alignment: .leading)
                        .font(.largeTitle)
                        .foregroundColor(.blue)
                }
            }
        }
    }
}

enter image description here

azamsharp
  • 19,710
  • 36
  • 144
  • 222