SwiftUI conditional view will not animate/transition
Place your .transition
on the container of the views that will switch, not each conditional view. Here's a trivial example from some code I have done (which works).
In the main View that needs to transition conditionally:
import SwiftUIstruct AppWrapperView: View { @State var showFirstRun:Bool = true var body: some View { ZStack { if (showFirstRun) { FirstRunView(showFirstRun: $showFirstRun) } else { Text("Some other view") } } .transition(.slide) }}
Then, somewhere in the view that triggers the change in condition:
import SwiftUIstruct FirstRunView: View { @Binding var showFirstRun:Bool var body: some View { Button(action: { withAnimation { self.showFirstRun = false } }) { Text("Done") } }}
I had to put my if..else
statement inside ZStack
container instead of Group
. Seems that Group
was the main reason for broken animation in my case. Also, I applied .transition
in combination with .animation
to container instead of views.
ZStack { if(isSignedIn){ Home() } else { AuthSignin(isSignedIn: self.$isSignedIn) }}.transition(.slide).animation(.easeInOut)