gpt4 book ai didi

ios - SwiftUI旋转屏幕使模式不再消失

转载 作者:行者123 更新时间:2023-12-01 21:18:09 26 4
gpt4 key购买 nike

我在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 13以来我有这个问题
我目前在iOS 14.2 Beta上
和Xcode 12 GM
[1]: 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")
}
}
}
}

关于ios - SwiftUI旋转屏幕使模式不再消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63985299/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com