I have a custom alert that I'm trying to slide down from the top of the screen on presentation. I'm using transition and the new .animation modifier for iOS 15:
import SwiftUI
struct SlideOverAlert: View {
var text: String
@Binding var isPresented: Bool
var dismissAction: () -> Void
var body: some View {
if isPresented {
ZStack {
Rectangle()
.edgesIgnoringSafeArea(.top)
.frame(height: 80)
.foregroundColor(.orange)
HStack {
Text(text)
Spacer()
Button(action: dismissAction) {
Image(systemName: "xmark")
}
}
.foregroundColor(.white)
.padding()
}
.transition(.move(edge: isPresented ? .top : .bottom))
.animation(.default, value: isPresented)
}
}
}
The code I have isn't working. And I can't figure out why...
Here's my preview code:
struct SlideOverAlert_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
struct ContentView: View {
@State var alertIsPresented = true
var body: some View {
GeometryReader { _ in
ZStack(alignment: .top) {
VStack {
Spacer()
Button(action: { alertIsPresented.toggle() }) {
Text("Toggle")
}
Spacer()
}
SlideOverAlert(
text: "That isn't going to work",
isPresented: $alertIsPresented
) {
alertIsPresented.toggle()
}
}
}
}
}
}