在 SwiftUI 的 SplitView 中导航到 master 内部而不是 detail

Navigate inside master instead of detail in SplitView in SwiftUI

我的 iPad 就绪应用程序中有拆分视图:

struct ContentView: View {
    var body: some View {
        NavigationView {
            List {
                NavigationLink("Show the slave view HERE", destination: SlaveView())
                    .navigationBarTitle("Master view")
            }

            Text("Detail view")
                .navigationBarTitle("DO NOT show the slave view here")
        }
    }
}

所以我喜欢 SlaveView 视图在列表本身中打开,而不是在详细视图中打开。 我已经尝试在 Slave 中设置另一个 NavigationView,也在其下方设置一个文本,还在每个 MasterSlave 上设置所有 navigationViewStyle 没有运气.

这是您可以构建的最简单的从属视图:

struct SlaveView: View {
    var body: some View {
        List {
            NavigationLink("Sub Detail view", destination: Text("Sub Detail view"))
        }
        .navigationBarTitle("Slave view")
    }
}

那么如何更改拆分视图的主视图(左)而不是详细视图(右)?

请注意这是一个简化的可重现代码。实际项目使用更复杂的主从列表等。此外,我们不想丢失导航内容,例如转换、标题转换、后退按钮等。

为了更清楚,我需要流程中的这个状态:

您可以尝试使用 Button 而不是 NavigationLink 并替换您的主视图:

struct ContentView: View {
    @State var showSlaveView = false

    var body: some View {
        NavigationView {
            masterView
                .navigationBarTitle("Master view")
            Text("Detail view")
                .navigationBarTitle("DO NOT show the slave view here")
        }
    }
}

extension ContentView {
    @ViewBuilder
    var masterView: some View {
        if showSlaveView {
            SlaveView()
                .onTapGesture { self.showSlaveView = false }
        } else {
            Button("Show the slave view HERE") {
                self.showSlaveView = true
            }
        }
    }
}

您可以尝试使用 StackNavigationViewStyle()

struct ContentView: View {
    var body: some View {
        NavigationView {
            NavigationLink("Show the slave view HERE", destination: SlaveView())
                .navigationBarTitle("Master view")
            
            Text("Detail view")
                .navigationBarTitle("DO NOT show the slave view here")
        }
        .navigationViewStyle(StackNavigationViewStyle())
    }
}

只是修改link不是细节

NavigationLink("Show the slave view HERE", destination: SlaveView())
    .isDetailLink(false)