Skip to content

Instantly share code, notes, and snippets.

@katsuyoshi
Created September 29, 2024 16:19
Show Gist options
  • Save katsuyoshi/8175488d81210d51c11aa9dd7779f4c1 to your computer and use it in GitHub Desktop.
Save katsuyoshi/8175488d81210d51c11aa9dd7779f4c1 to your computer and use it in GitHub Desktop.
If the alert isn’t shown, set the show flag later. I guess the alert view hides behind some view when the view creates complexity.
import SwiftUI
struct ContentView: View {
@State var isPresented = false
var body: some View {
VStack {
Image(systemName: "globe")
.imageScale(.large)
.foregroundStyle(.tint)
Text("Hello, world!")
}
.padding()
.onAppear() {
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1) {
isPresented.toggle()
}
}
.alert("Alert", isPresented: $isPresented) {
Button("OK") {
isPresented = false
}
} message: {
Text("Alert message.")
}
}
}
#Preview {
ContentView()
}
@katsuyoshi
Copy link
Author

The point is:

            DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1) {
                isPresented.toggle()
            }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment