TienTranMinh
TienTranMinh

Reputation: 172

ToolbarColorScheme change when back to previous screen programmatically using NavigationStack

I'm using navigationStack and I found that when I back programmatically using navigationPath instead of using default back button on navigation bar. My ToolbarColorScheme is change from dark to white.
Here is my demo to reproduce this bug, for some reasone I want to use navigationPath to control app navigation because it's can help me with some case like popToRoot() or pop to exact some screen. So can anyone can help me with this. Thank you!

struct TestView: View {
    
    private var bgColors: [Color] = [ .indigo, .yellow, .green, .orange, .brown ]
    
    @State private var path: [Color] = []
    
    var body: some View {
        NavigationStack(path: $path) {
            List(bgColors, id: \.self) { bgColor in
                NavigationLink(value: bgColor) {
                    Text(bgColor.description)
                }
            }
            .toolbarColorScheme(.dark, for: .navigationBar)
            .toolbarBackground(
                .blue,
                for: .navigationBar)
            .toolbarBackground(.visible, for: .navigationBar)
            .listStyle(.plain)
            .navigationDestination(for: Color.self) { color in
                VStack {
                    Text("\(path.count), \(path.description)")
                        .font(.headline)
                    HStack {
                        ForEach(path, id: \.self) { color in
                            color
                                .frame(maxWidth: .infinity, maxHeight: .infinity)
                        }
                    }
                    Button {
                        path.removeLast()
                    } label: {
                        Text("Back")
                    }
                }
                .toolbarColorScheme(.dark, for: .navigationBar)
                .toolbarBackground(
                    .blue,
                    for: .navigationBar)
                .toolbarBackground(.visible, for: .navigationBar)
            }
            .navigationTitle("Color")
        }
    }
}

Default state
enter image description here
When back by press back button
enter image description here

Upvotes: 0

Views: 218

Answers (1)

TKumar
TKumar

Reputation: 836

Just comment out 3 lines from your codes. I guess that would solve your problem.

struct TestView: View {
    
    private var bgColors: [Color] = [ .indigo, .yellow, .green, .orange, .brown ]
    
    @State private var path: [Color] = []
    
    var body: some View {
        NavigationStack(path: $path) {
            List(bgColors, id: \.self) { bgColor in
                NavigationLink(value: bgColor) {
                    Text(bgColor.description)
                }
            }
            .toolbarColorScheme(.dark, for: .navigationBar)
            .toolbarBackground(
                .blue,
                for: .navigationBar)
            .toolbarBackground(.visible, for: .navigationBar)
            .listStyle(.plain)
            .navigationDestination(for: Color.self) { color in
                VStack {
                    Text("\(path.count), \(path.description)")
                        .font(.headline)
                    HStack {
                        ForEach(path, id: \.self) { color in
                            color
                                .frame(maxWidth: .infinity, maxHeight: .infinity)
                        }
                    }
                    Button {
                        path.removeLast()
                    } label: {
                        Text("Back")
                    }
                }
                //.toolbarColorScheme(.dark, for: .navigationBar)
                //.toolbarBackground(
                //   .blue,
                //   for: .navigationBar)
                //.toolbarBackground(.visible, for: .navigationBar)
            }
            .navigationTitle("Color")
        }
    }
}

Upvotes: -1

Related Questions