TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

Override accentColor for a particular view

Forums > SwiftUI

I have a view that I would like to be able to change the accentColor for only in that particular view. More specifically, the "<- Back" button that is automatically generated by the system. I need to change its color for only one view. I appreciate any tips.

2      

Hi! One of the ways I suppose like this.

struct ContentView: View {
    var body: some View {
        NavigationStack {
            NavigationLink("Go To Second View") {
                DetailView()
            }
        }
    }
}

struct DetailView: View {
    // You are responsible for dismissing the view
    // Shoud you have many subviews you will have to
    // handle dismiss of those yourself
    @Environment(\.dismiss) var dimiss
    // Store it in UserDefaults so that it keeps in memory your choice
    // once you go back to that view
    @AppStorage("tintColor") var tintColor = false

    var body: some View {
        Toggle("Change Tint", isOn: $tintColor)
            .padding(.horizontal)
        // hide default button
        .navigationBarBackButtonHidden()
        // provide your own button
        .toolbar {
            ToolbarItem(placement: .topBarLeading) {
                Button {
                    dimiss()
                } label: {
                    HStack(spacing: 5) {
                        Image(systemName: "chevron.left")
                        Text("Back")
                    }
                }
                // here you monitor the tint for the button in this particular view
                .tint(tintColor ? .orange : .blue)
            }
        }
    }
}

2      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.