SwiftUI 关闭 NavigationView 呈现的模式 sheet(Xcode Beta 5)
SwiftUI dismiss modal sheet presented from NavigationView (Xcode Beta 5)
我正试图关闭通过 SwiftUI 中的 .sheet
呈现的模式视图 - 由 NavigationView
s navigationBarItems
内的 Button
调用,按照以下:
struct ModalView : View {
@Environment(\.presentationMode) var presentationMode
var body: some View {
Button(action: {
self.presentationMode.value.dismiss()
}, label: { Text("Save")})
}
}
struct ContentView : View {
@State var showModal: Bool = false
var body: some View {
NavigationView {
Text("test")
.navigationBarTitle(Text("Navigation Title Text"))
.navigationBarItems(trailing:
Button(action: {
self.showModal = true
}, label: { Text("Add") })
.sheet(isPresented: $showModal, content: { ModalView() })
)
}
}
}
点击“保存”按钮时,模态框不会消失,它只是保留在屏幕上。摆脱它的唯一方法是向下滑动模态。
打印self.presentationMode.value
的值总是显示false
所以好像还以为没有显示出来
只有在 NavigationView
中出现时才会发生这种情况。把它拿出来,它工作正常。
我是不是遗漏了什么,或者这是一个测试版问题?
您需要将 .sheet
移到 Button 之外。
NavigationView {
Text("test")
.navigationBarTitle(Text("Navigation Title Text"))
.navigationBarItems(trailing:
Button("Add") {
self.showModal = true
}
)
.sheet(isPresented: $showModal, content: { ModalView() })
}
您甚至可以将它移到 NavigationView
闭包之外。
NavigationView {
Text("test")
.navigationBarTitle(Text("Navigation Title Text"))
.navigationBarItems(trailing:
Button("Add") { self.showModal = true }
)
}
.sheet(isPresented: $showModal, content: { ModalView() })
请注意,如果您有一个简单的文本按钮,您还可以简化按钮调用。
解决方案在文档中并不明显,大多数教程都选择简单的解决方案。但我真的想要 sheet 的 NavigationBar 中的一个按钮来关闭 sheet。以下是六个步骤的解决方案:
- 将 DetailView 设置为不显示。
- 添加一个按钮以设置要显示的 DetailView。
- 调用 .sheet(isPresented 修饰符以显示 sheet.
- 将显示在 sheet 中的视图包装在 NavigationView 中,因为我们要显示 .navigationBarItem 按钮。
- 关闭 sheet 视图需要 PresentationMode。
- 向 NavBar 添加一个按钮并调用 dismiss 方法。
导入 SwiftUI
struct ContentView: View {
// 1
@State private var showingDetail = false
var body: some View {
VStack {
Text("Hello, world!")
.padding()
Button("Show Detail") {
showingDetail = true // 2
}
// 3
.sheet(isPresented: $showingDetail) {
// 4
NavigationView {
DetailView()
}
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
struct DetailView: View {
// 5
@Environment(\.presentationMode) var presentationMode
var body: some View {
Text("Detail View!")
// 6
.navigationBarItems(leading: Button(action: {
presentationMode.wrappedValue.dismiss()
}) {
Image(systemName: "x.circle")
.font(.headline)
.foregroundColor(.accentColor)
})
}
}
我正试图关闭通过 SwiftUI 中的 .sheet
呈现的模式视图 - 由 NavigationView
s navigationBarItems
内的 Button
调用,按照以下:
struct ModalView : View {
@Environment(\.presentationMode) var presentationMode
var body: some View {
Button(action: {
self.presentationMode.value.dismiss()
}, label: { Text("Save")})
}
}
struct ContentView : View {
@State var showModal: Bool = false
var body: some View {
NavigationView {
Text("test")
.navigationBarTitle(Text("Navigation Title Text"))
.navigationBarItems(trailing:
Button(action: {
self.showModal = true
}, label: { Text("Add") })
.sheet(isPresented: $showModal, content: { ModalView() })
)
}
}
}
点击“保存”按钮时,模态框不会消失,它只是保留在屏幕上。摆脱它的唯一方法是向下滑动模态。
打印self.presentationMode.value
的值总是显示false
所以好像还以为没有显示出来
只有在 NavigationView
中出现时才会发生这种情况。把它拿出来,它工作正常。
我是不是遗漏了什么,或者这是一个测试版问题?
您需要将 .sheet
移到 Button 之外。
NavigationView {
Text("test")
.navigationBarTitle(Text("Navigation Title Text"))
.navigationBarItems(trailing:
Button("Add") {
self.showModal = true
}
)
.sheet(isPresented: $showModal, content: { ModalView() })
}
您甚至可以将它移到 NavigationView
闭包之外。
NavigationView {
Text("test")
.navigationBarTitle(Text("Navigation Title Text"))
.navigationBarItems(trailing:
Button("Add") { self.showModal = true }
)
}
.sheet(isPresented: $showModal, content: { ModalView() })
请注意,如果您有一个简单的文本按钮,您还可以简化按钮调用。
解决方案在文档中并不明显,大多数教程都选择简单的解决方案。但我真的想要 sheet 的 NavigationBar 中的一个按钮来关闭 sheet。以下是六个步骤的解决方案:
- 将 DetailView 设置为不显示。
- 添加一个按钮以设置要显示的 DetailView。
- 调用 .sheet(isPresented 修饰符以显示 sheet.
- 将显示在 sheet 中的视图包装在 NavigationView 中,因为我们要显示 .navigationBarItem 按钮。
- 关闭 sheet 视图需要 PresentationMode。
- 向 NavBar 添加一个按钮并调用 dismiss 方法。
导入 SwiftUI
struct ContentView: View {
// 1
@State private var showingDetail = false
var body: some View {
VStack {
Text("Hello, world!")
.padding()
Button("Show Detail") {
showingDetail = true // 2
}
// 3
.sheet(isPresented: $showingDetail) {
// 4
NavigationView {
DetailView()
}
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
struct DetailView: View {
// 5
@Environment(\.presentationMode) var presentationMode
var body: some View {
Text("Detail View!")
// 6
.navigationBarItems(leading: Button(action: {
presentationMode.wrappedValue.dismiss()
}) {
Image(systemName: "x.circle")
.font(.headline)
.foregroundColor(.accentColor)
})
}
}