Xcode Beta 6 中的 SwiftUI 模态?
SwiftUI modals in Xcode Beta 6?
以前在 SwiftUI (Xcode Beta 5) 中,模态是这样工作的:
struct ContentView: View {
@State var modalIsPresented: Bool = false
var body: some View {
Button(action: {
self.modalIsPresented = true
}) {
Text("Show modal")
}
.sheet(isPresented: $modalIsPresented, content: {
ModalView()
})
}
}
struct ModalView: View {
@Environment(\.presentationMode) var presentationMode
var body: some View {
Button(action: {
self.presentationMode.value.dismiss()
}) {
Text("Hide modal")
}
}
}
但现在在 Xcode Beta 6 中,我找不到关闭模态的方法。 presentationMode
的value
属性已经没有了,其他的属性好像也没有什么有用的方法可以用了
如何关闭 Xcode Beta 6 中的 SwiftUI 模式?
您可以通过传入控制其显示的绑定来关闭 .sheet
、.popover
、.actionSheet
,此处 $modalIsPresented
并将其设置为 false 以编程方式关闭它。
使用 wrappedValue 代替 value似乎适用于 Xcode Beta 6:
self.presentationMode.wrappedValue.dismiss()
尝试检查一下:
.presentation(showModal ? Modal(Text("Modal screen"), onDismiss: {
self.showModal.toggle()
}) : nil)
默认的模态显示并没有证明用户可以通过任何视觉方式关闭模态,但是从 iOS 13 开始,用户可以向下滑动视图使其消失。
详细:https://alejandromp.com/blog/2019/06/24/improving-swiftui-modal-presentation-api/
以前在 SwiftUI (Xcode Beta 5) 中,模态是这样工作的:
struct ContentView: View {
@State var modalIsPresented: Bool = false
var body: some View {
Button(action: {
self.modalIsPresented = true
}) {
Text("Show modal")
}
.sheet(isPresented: $modalIsPresented, content: {
ModalView()
})
}
}
struct ModalView: View {
@Environment(\.presentationMode) var presentationMode
var body: some View {
Button(action: {
self.presentationMode.value.dismiss()
}) {
Text("Hide modal")
}
}
}
但现在在 Xcode Beta 6 中,我找不到关闭模态的方法。 presentationMode
的value
属性已经没有了,其他的属性好像也没有什么有用的方法可以用了
如何关闭 Xcode Beta 6 中的 SwiftUI 模式?
您可以通过传入控制其显示的绑定来关闭 .sheet
、.popover
、.actionSheet
,此处 $modalIsPresented
并将其设置为 false 以编程方式关闭它。
使用 wrappedValue 代替 value似乎适用于 Xcode Beta 6:
self.presentationMode.wrappedValue.dismiss()
尝试检查一下:
.presentation(showModal ? Modal(Text("Modal screen"), onDismiss: {
self.showModal.toggle()
}) : nil)
默认的模态显示并没有证明用户可以通过任何视觉方式关闭模态,但是从 iOS 13 开始,用户可以向下滑动视图使其消失。
详细:https://alejandromp.com/blog/2019/06/24/improving-swiftui-modal-presentation-api/