0

In my app I have a NavigationStack inside the detail of a NavigationSplitView. With the code below the navigation back and forward works fine but if from the detail, I incompletely swipe left to dismiss the view, the NavigationLink doesn't work anymore. This code is partially copied from Apple Documentation. Does anyone know what causes the problem?

enter image description here

struct ContentView: View {
    let colors: [Color] = [.purple, .pink, .orange]
    @State private var selection: Color? = nil
    
    var body: some View {
        NavigationSplitView {
            List(colors, id: \.self, selection: $selection) { color in
                NavigationLink(color.description, value: color)
            }
        } detail: {
            NavigationStack {
                if let color = selection {
                    VStack {
                        NavigationLink(color.description, value: color)
                    }
                    .navigationDestination(for: Color.self) { color in
                        RoundedRectangle(cornerRadius: 10)
                            .fill(color)
                            .frame(width: 150, height: 150)
                    }
                } else {
                    Text("Pick a color")
                }
            }
        }
    }
}

EDIT: I've made some code improvements creating separate struct for each view to avoid confusion and better understand the code but the problem persist.

struct ContentView: View {
    let colors: [Color] = [.purple, .pink, .orange]
    @State private var selection: Color? = nil
    
    var body: some View {
        NavigationSplitView {
            List(colors, id: \.self, selection: $selection) { color in
                NavigationLink(color.description, value: color)
            }
        } detail: {
            NavigationStack {
                if let color = selection {
                    DetailView(color: color)
                } else {
                    Text("Pick a color")
                }
            }
        }
    }
}
struct DetailView: View {
    let color: Color
    
    var body: some View {
        VStack {
            NavigationLink("Go to third view", value: color)
                .padding()
                .background(color)
                .foregroundColor(.white)
        }
        .navigationTitle("Detail View")
        .navigationDestination(for: Color.self) { color in
            ThirdView(color: color)
        }
    }
}
struct ThirdView: View {
    let color: Color
    
    var body: some View {
        RoundedRectangle(cornerRadius: 10)
            .fill(color)
            .frame(width: 150, height: 150)
            .navigationTitle("Third View")
    }
}
Gaspare
  • 103
  • 5
  • Seems like a bug related to the new navigation setup, I’ve seen a few different versions of the same thing – lorem ipsum Oct 18 '22 at 11:18
  • May be the NavigationStack in the detail view is the problem : you have 2 different navigation handler working at the same time. – Ptit Xav Oct 18 '22 at 12:15
  • @PtitXav From the NavigationSplitView documentation: "You can also embed a NavigationStack in a column. Tapping or clicking a NavigationLink that appears in an earlier column sets the view that the stack displays over its root view. Activating a link in the same column adds a view to the stack. Either way, the link must present a data type for which the stack has a corresponding navigationDestination(for:destination:) modifier.". – Gaspare Oct 18 '22 at 13:47
  • Did not know. In your code, both navigation link seem identical. May this confuse the system ? – Ptit Xav Oct 18 '22 at 14:32
  • @PtitXav In my opinion it's a bug, because the navigation works fine until the user don't complete the swipe. This is not a common scenario but can happen. – Gaspare Oct 18 '22 at 15:12

1 Answers1

0

It looks like the bug is fixed. At least it works as expected on the latest Xcode 14.1 RC.

Mecid
  • 4,491
  • 6
  • 30
  • 30