Popovers
Popovers copied to clipboard
Cannot auto alert when present init true
Use SwiftUI API can auto alert when page first loaded, but your code cannot.
SwiftUI API code: @State private var showAlert = true VStack(spacing: 0) { Text("首次启动弹出弹窗") } .alert("title", isPresented: $showAlert, actions: { Button("取消", role: .cancel, action: {}) }, message: { Text("Message") })
Your code: @State private var showAlert = true VStack(spacing: 0) { Text("首次启动弹出弹窗") } .popover(present: $showAlert) { Text("Message") }
This is because Popovers listens to changes in present
- you'll need to attach an onAppear
and set showAlert
there.
@State private var showAlert = false /// set to false first
.onAppear {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
showAlert = true /// show it!
}
}