Search code examples
swiftuiback-button

How to change color of back button on NavigationView


When you click on the button it takes you to a new view and puts a back button in the top left. I can't figure out what property controls the color of the back button. I tried adding an accentColor and foregroundColor but they only edit the items inside the view.

var body: some View {
    NavigationView {
        NavigationLink(destination: ResetPasswordView()) {
            Text("Reset Password")
            .foregroundColor(Color(red: 0, green: 116 / 255, blue: 217 / 255))
            .padding()
        }
    }
}

Solution

  • You can use the accentColor property on the NavigationView to set the back button color, like in this example:

    var body: some View {
       NavigationView {
          List(1..<13) { item in
             NavigationLink(destination: Text("\(item) x 8 = \(item*8)")) {
                  Text(String(item))
                }
          }.navigationBarTitle("Table of 8")
       }.accentColor(.orange) // <- note that it's added here and not on the List like navigationBarTitle()
    }
    

    It works the same for NavigationStack

    Update

    Xcode 15.2 is displaying a deprecation warning indicating that .accentColor will be deprecated in future iOS versions. The recommended use is now with the .tint modifier as outlined below.

       NavigationView {
          // [...]
       }.tint(.orange)