0

I have the following view inside WindowGroup:

struct SidebarSelection: View {
    
    @State var selection: Int?
    @State var path: [Int] = []
    
    var body: some View {
        NavigationSplitView {
            List(1...20, id: \.self, selection: $selection) { number in
                    Text("I like \(number)")
            }
        } detail: {
            NavigationStack(path: $path) {
                VStack {
                    Image(systemName: "x.squareroot")
                        .imageScale(.large)
                        .foregroundColor(.accentColor)
                    Text("This is the NavigationStack root")
                }
                .padding()
                .navigationDestination(for: Int.self) { number in
                    Text("You chose \(number)")
                }
            }
        }
        .onChange(of: selection) { newValue in
            print("You clicked \(newValue ?? 0)")
            if let newValue {
                path.append(newValue)
                print("New path \(path)")
            }
        }
        .onChange(of: path) { newValue in
            print("Path changed to \(path)")
        }
    }
}

If I run this on macOS and click:

  1. „I like 5“
  2. „I like 6“
  3. „I like 7“

I would expect the detail view to show:

  1. „You chose 5“
  2. „You chose 6“
  3. „You chose 7“

And the console to show:

You clicked 5
New path [5]
Path changed to [5]
You clicked 6
New path [5, 6]
Path changed to [5, 6]
You clicked 7
New path [5, 6, 7]
Path changed to [5, 6, 7]

What I actually see in the detail view is:

  1. „You chose 5“
  2. „This is the NavigationStack root“
  3. „You chose 7“

And the console shows:

New path [5]
Path changed to [5]
You clicked 6
New path [5, 6]
Path changed to []
You clicked 7
New path [7]
Path changed to [7]

I think this is a bug and have filed a feedback and also asked on the Apple forums half a year ago but the problem persists.

Am I doing something wrong? Does anyone know what emptied the path and/or how to properly use a NavigationStack inside a NavigationSplitView?

Dominik
  • 706
  • 7
  • 22
  • can you show what your newvalue is in onchange of path – andylee May 26 '23 at 09:25
  • In the example above it is `[5]`, `[]` and `[7]`. – Dominik May 26 '23 at 10:12
  • looks like a lot of people are having problems with using split view and stack together. My guess is that since view inside navigation stack is keep redrawn if embedded in detail section of split view, but I can only guess. If you just can use splitview and stack separetly that will probably solve the problem. – andylee May 26 '23 at 16:57

0 Answers0