Oxford212
Oxford212

Reputation: 129

SwiftUI pop out temporary alert

I tried to do a app that pop out a temporary alert that only appear for 1 or 2 seconds. It’s something like App Store rating. enter image description here

But I don’t know what this called in swiftui. Can anyone answer me?

Upvotes: 1

Views: 1451

Answers (1)

vacawama
vacawama

Reputation: 154583

That is just a view that is shown or hidden conditionally. Here is a complete example that uses a ZStack to place the thank you view over the other view content. The thank you view is either present or not based upon the @State variable showThankYou. DispatchQueue.main.asyncAfter is used to remove the view after 3 seconds.

struct ContentView: View {
    @State private var showThankYou = false
    
    var body: some View {
        ZStack {
            VStack {
                Spacer()
                Text("Stuff in the view")
                Spacer()
                Button("submit") {
                    showThankYou = true
                    DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
                        self.showThankYou = false
                    }
                }
                Spacer()
                Text("More stuff in the View")
                Spacer()
            }
            if showThankYou {
                RoundedRectangle(cornerRadius: 16)
                    .foregroundColor(Color.gray)
                    .frame(width: 250, height: 250)
                    .overlay(
                        VStack {
                            Text("Submitted").font(.largeTitle)
                            Text("Thanks for your feedback").font(.body)
                        }
                    )
            }
        }
    }
}

Upvotes: 3

Related Questions