3

我的 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在列表本身中打开视图,而不是在详细视图中。我尝试在 中设置另一个NavigationViewSlave下面还有一个文本,同时在两个和每个上都设置了所有s navigationViewStyle,但没有运气。MasterSlave

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

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

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

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

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

需要的步骤

4

3 回答 3

4

只需修改不是详细信息的链接

演示

NavigationLink("Show the slave view HERE", destination: SlaveView())
    .isDetailLink(false)
于 2020-07-30T02:00:49.413 回答
0

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

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
            }
        }
    }
}
于 2020-07-29T22:39:08.293 回答
-1

您可以尝试使用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())
    }
}
于 2020-07-29T22:44:00.790 回答