ios - SwiftUI 旋转屏幕使模态不再自行消失
问题描述
我在 SwiftUI 上有一个错误,当我旋转我的设备时,模态不再消失,这里的问题是只发生在模拟器上的设备上也适用于我的 iPad。
import SwiftUI
struct modalView: View {
@Environment(\.presentationMode) var presentationMode
var body: some View {
Button(action:{
self.presentationMode.wrappedValue.dismiss()
}){
Text("close")
}
}
}
struct ContentView: View {
@State var showModal = false
var body: some View {
Button(action: {
showModal.toggle()
}){
Text("modal")
}
.sheet(isPresented: self.$showModal, content: {
modalView()
})
}
}
[我设备上的错误][1]
我有这个问题,因为我目前在 iOS 14.2 beta 和 Xcode 12 GM [1] 上使用 iOS 13:https ://twitter.com/MisaelLandero/status/1306953785651142656?s=20
解决方案
尝试使用这样的东西:
struct ContentView: View {
@State private var showModal = false
// If you are getting the "can only present once" issue, add this here.
// Fixes the problem, but not sure why; feel free to edit/explain below.
@Environment(\.presentationMode) var presentationMode
var body: some View {
Button(action: {
self.showModal = true
}) {
Text("Show modal")
}.sheet(isPresented: self.$showModal) {
ModalView()
}
}
}
struct ModalView: View {
@Environment(\.presentationMode) private var presentationMode
var body: some View {
Group {
Text("Modal view")
Button(action: {
self.presentationMode.wrappedValue.dismiss()
}) {
Text("Dismiss")
}
}
}
}
推荐阅读
- reactjs - How to get queryset in django rest framework for foreignkey?
- javascript - Facebook Login 不断重复询问权限 React Native?
- oracle - Not able to make new oracle db connection in sqldeveloper
- javascript - How do I create an express route that handles an HTTP GET request which sends an a href tag
- spring-boot - Vaadin BeanCreationException: during trying to call save method of my service class
- javascript - 如何制作和不和谐自动删除角色的 js 事件?
- php - 在 Laravel 6.2 中从空值创建默认对象
- android - 如何使用 Cloud Functions (Typescript) 在 Firebase 实时数据库中的 .ref(/path) 之外创建新的父节点?
- wordpress - How to show custom taxonomy in tab not working when using with wp post query?
- graph - 有趣的 Python 数据结构问题,涉及不相交集、散列和图