In SwiftUI, how do you make a view fade or slide in and out when it is shown or hidden with transition(_:)?
answer
- arrival and departure, not a property change
- a conditional view inside an if
- transition(_:) on that view
- toggle inside withAnimation
basics
~10 sShow the view inside an if, attach transition(.opacity) or transition(.move(edge: .top)) to it, and toggle the Boolean inside withAnimation. SwiftUI then animates the view in on insertion and out on removal.
solid answer
~30 sA transition describes how a view enters and leaves the hierarchy, so the view must actually come and go: wrap it in `if showBanner { ... }`. Attach `.transition(.opacity)`, `.transition(.move(edge: .top))`, `.transition(.scale)` or `.transition(.slide)` to the conditional view, and combine two effects with `combined(with:)`, for example `.move(edge: .top).combined(with: .opacity)`. Then make the change animated with `withAnimation { showBanner.toggle() }`, or with `animation(_:value:)` on a container that is on screen in both states. Without an animation the view simply pops in and out; without the `if` there is no insertion for the transition to play on.
code
swift · 20 linesstruct SavedBanner: View {
@State private var showBanner = false
var body: some View {
VStack {
if showBanner {
Text("Saved")
.padding()
.background(.green, in: .capsule)
.transition(.move(edge: .top).combined(with: .opacity))
}
Spacer()
Button("Save") {
withAnimation {
showBanner.toggle()
}
}
}
}
}go deeper
Recall the three pieces: a view inside an if, a transition(_:) on that view, and a state change wrapped in withAnimation. Name opacity, move(edge:), scale and slide.
Explain that a transition only describes insertion and removal and that the animation comes from the transaction. Show combined(with:) and where animation(_:value:) must sit to see both states.
Diagnose a transition that never plays by checking each piece in turn: is the view really removed, is the change animated, is the modifier on the right view. Mention what replaced the deprecated value-less animation.
Frame transitions as part of a team's motion vocabulary: a small set of shared transitions applied consistently beats ad hoc effects per screen, and reviewers should check that each one is attached to a view that truly comes and goes.
## What a transition is In SwiftUI a **transition** describes how a view *enters* and *leaves* the view hierarchy. It is not an animation by itself: it says what the view looks like just before it is inserted and just after it is removed, and the current animation carries the view between that look and its normal appearance. You attach one with the `transition(_:)` modifier. Apple's documentation for the modifier says it plainly: when this view appears or disappears, the transition is applied to it, allowing it to animate in and out. That definition explains almost every beginner surprise. A transition only has something to do when a view *appears* or *disappears*. A view that stays on screen and merely changes its opacity, offset or color is animated by those modifiers, not by its transition. ## Three pieces that must all be there A working fade or slide needs three things at once: 1. **A view that is really inserted and removed.** The usual form is a conditional: `if showBanner { BannerView() }`. When `showBanner` becomes `true`, SwiftUI inserts the view; when it becomes `false`, SwiftUI removes it. 2. **A transition on that conditional view.** Attach `.transition(.opacity)`, `.transition(.move(edge: .top))`, `.transition(.scale)` or another built-in value to the view inside the `if`, not to some unrelated sibling. 3. **An animated state change.** Flip the Boolean inside `withAnimation { showBanner.toggle() }`, or attach `animation(_:value:)` to a container that is on screen in both states, such as the enclosing `VStack`. Without an animation, SwiftUI still applies the change, but the view simply pops in and out. If any one of the three is missing, the transition looks broken even though every line compiles. ## The built-in transitions SwiftUI ships a small set of ready-made transitions. Since iOS 17 they are values of concrete types conforming to the `Transition` protocol; the older type-erased `AnyTransition` offers the same names and still works. | Transition | What the inserted view does | |---|---| | `.opacity` | Fades from transparent to opaque, and back on removal | | `.move(edge:)` | Moves in from the given edge and out to it | | `.slide` | Slides in from one side and out the other | | `.scale` / `.scale(_:anchor:)` | Grows from a smaller scale, shrinking on removal | | `.offset(x:y:)` | Moves in from an offset you choose | | `.blurReplace` | Blurs and fades while it is replaced (iOS 17) | | `.identity` | No visual change at all | ## Combining and placing the modifier - **Combine two effects** with `combined(with:)`: `.move(edge: .top).combined(with: .opacity)` slides and fades at the same time, on both insertion and removal. - **Place `transition(_:)` on the conditional view**, the one the `if` creates. A transition on the parent stack describes how the *stack* enters and leaves, which is not what changed. - **Put the animation where it can see both states.** `withAnimation` wraps the state change itself, so it works from a button action. `animation(_:value:)` belongs on a container that exists before and after the change. - **Use different effects for entering and leaving** with an asymmetric transition, for example a toast that moves in from the top but fades out; that variant is the next step after the basics. ## A worked example A "Saved" banner that drops in from the top and fades, toggled by a button: ```swift struct SavedBanner: View { @State private var showBanner = false var body: some View { VStack { if showBanner { Text("Saved") .padding() .background(.green, in: .capsule) .transition(.move(edge: .top).combined(with: .opacity)) } Spacer() Button("Save") { withAnimation { showBanner.toggle() } } } } } ``` Tapping the button changes `showBanner` inside an animated transaction. On insertion the banner starts above its slot and transparent, then animates into place; on removal it runs the same effect in reverse. Swap `withAnimation` for `withAnimation(.snappy)` to change the timing; the transition decides *what* changes, the animation decides *how fast and with which curve*. ## Mistakes to avoid - **Hiding with `opacity(0)` and expecting a transition.** The view is still in the hierarchy, so only the opacity value animates and the transition is never consulted. - **Changing state without any animation.** The view appears and disappears instantly; the transition has no animation to ride on. - **Attaching the transition to the wrong view.** A transition on the `VStack` does nothing for a child that comes and goes. - **Reaching for the deprecated value-less `animation(_:)`.** It has been deprecated since iOS 15 in favour of `withAnimation` or `animation(_:value:)`. - **Using a transition to animate a value inside a view that stays**, such as a counter's digits. That is a content change inside one view, animated by other tools, not an insertion. Remember the one-line rule: a transition is how a view arrives and departs, so give SwiftUI an arrival or a departure, and make the change animated.
- Why does the banner pop in instantly if you remove withAnimation but keep the transition?A transition only describes the starting and ending look of an inserted or removed view; the animation in the current transaction moves it between them. With no animation, SwiftUI applies the insertion immediately, so the view appears in its final state. Wrap the change in `withAnimation`, or add `animation(_:value:)` on a container present in both states.
- What does combined(with:) do, and how is it different from using two transition modifiers?`combined(with:)` builds one transition that applies both effects together, such as `.move(edge: .top).combined(with: .opacity)`, so the view slides and fades on both insertion and removal. It is the supported way to merge effects; when entering and leaving should look different, use an asymmetric transition rather than stacking modifiers.
- Adding transition(_:) alone makes the view animate in, no withAnimation needed.
- Setting opacity to zero on a view runs its removal transition.
- The transition modifier belongs on the parent stack, not on the conditional view.
- The value-less animation(_:) modifier is the current way to animate appearance.
- A transition plays whenever any property of the view changes.