我有很严重的问题。我的Xcode版本是13,iOS版本是15。
import SwiftUI
struct ContentView: View {
@State var isGo: Bool = false
var body: some View {
ZStack {
Button(action: {
self.isGo = true
}, label: {
Text("Go EmptyView")
})
EmptyView()
.background(Color.green)
.frame(width: 100, height: 100)
.sheet(isPresented: $isGo, onDismiss: nil, content: {
PopupView()
})
}
}
}
struct PopupView: View {
var body: some View {
Rectangle()
.fill(Color.green)
}
}上面的代码不起作用。但是,以前的Xcode版本或以前的iOS版本是代码正在工作。这是iOS的bug吗?有什么解决方案吗?
发布于 2021-09-28 05:54:32
你已经很接近了,不知道为什么没有人提供帮助。您的代码确实可以工作,但理论上是不正确的,您需要将调用.sheet的位置移到ZStack之外,它才能工作。但这里有一个更好的方法,没有所有无用的代码。
struct ContentView: View {
@State var showemptyview: Bool = false
var body: some View {
Button("Go EmptyView") {
showemptyview.toggle()
}
.sheet(isPresented: $showemptyview) {
EmptyView()
.background(Color.green)
}
}
}

发布于 2021-09-30 10:09:12
最后,在最后一个版本中,您可以直接使用几个工作表作为主视图,并且它可以工作。您不需要使用sheet创建单独的EmptyView()
YourMainView()
.sheet(item: $viewModel) { item in
// some logic here
}
.sheet(isPresented: $onther_viewModel.showView, content: {
SomeAnotherView(viewModel: viewModel.getVM())
})
.sheet(isPresented: $onther_viewModel2.showView, content: {
SomeView(viewModel: viewModel.getVM())
})https://stackoverflow.com/questions/69354864
复制相似问题