如何解散DetailView不关闭整个窗口在SwiftUI MacOS应用程序?



下面是两个SwiftUI视图的示例,它们的意外行为是关闭窗口而不是解散DetailView:

struct ContentView: View {
var body: some View {
NavigationView {
NavigationLink(destination: DetailView()) {
Text("Go to detail view")
}
}
}
}
struct DetailView: View {
@Environment(.presentationMode) var presentationMode: Binding<PresentationMode>

var body: some View {
Button(action: { presentationMode.wrappedValue.dismiss() }) {
Text("Dismiss")
}
}
}

这是显示这种行为的视频。

你也可以使用NavigationSplitView让你知道NavigationView现在不支持了

struct ContentView: View {
@State private var isDetailViewPresented = false
var body: some View {
NavigationSplitView {
Button("Go to detail view") {
isDetailViewPresented = true
}
} detail: {
if  isDetailViewPresented {
DetailView(isPresented: $isDetailViewPresented)
}
}
}
}


struct DetailView: View {
@Binding var isPresented: Bool
var body: some View {
VStack {
Text("Detail View")
Button(action: { isPresented.toggle()
}) {
Text("Dismiss")
}
}
}
}

相关内容

  • 没有找到相关文章

最新更新