In SwiftUI, a tap should animate an expanding card's height, but the card jumps straight to its final size; what do you check, and in what order?
answer
- is the change inside the transaction
- after an await is a later update
- the watched value and the wrapped modifiers
- something below sets the animation to nil
- interpolation versus insertion
basics
~20 sCheck that the mutation runs synchronously inside withAnimation (not after an await), that any animation(_:value:) watches the value that changed and wraps the changing modifier, and that nothing below sets the transaction's animation to nil.
solid answer
~50 sA jump means the update that changed the height carried no animation for that view. First I check where the mutation happens: `withAnimation` only covers mutations made while its synchronous closure runs, so `withAnimation { Task { await load(); isExpanded = true } }` animates nothing; the fix is to await first, then wrap the mutation. Second, if the card relies on `animation(_:value:)`, I check that it watches the value that actually changed and that it is written after the `frame` or layout modifier that changes. Third, I look for an override lower down, such as `.transaction { $0.animation = nil }` or `.animation(nil, value:)`. Last, I check that the change is an interpolation at all: if the card is replaced through an `if` branch or a changed `id(_:)`, it is an insertion and removal, which transitions handle.
code
swift · 27 linesstruct DetailsCard: View {
@State private var details = ""
@State private var isExpanded = false
var body: some View {
VStack(alignment: .leading) {
Button("Show details") {
Task {
let loaded = await loadDetails()
// The mutation runs inside withAnimation, after the await.
withAnimation(.smooth) {
details = loaded
isExpanded = true
}
}
}
Text(details)
.frame(height: isExpanded ? 160 : 0, alignment: .top)
.clipped()
}
}
private func loadDetails() async -> String {
try? await Task.sleep(for: .milliseconds(300))
return "Ships in two days. Free returns for 30 days."
}
}go deeper
Recall that only state changed inside withAnimation's closure, or watched by animation(_:value:), animates.
Explain why work after an await is a separate update and how the value and placement of animation(_:value:) decide what it reaches.
Walk an ordered diagnosis: mutation site, value and placement, overrides in the hierarchy, then identity versus interpolation, and say what each fix changes.
Be ready to describe conventions that prevent these bugs, such as animating state changes at one place in a view model's action and banning container-wide animation overrides.
## What a jump tells you SwiftUI animates a state change when the update that applies it carries an **animation** in its **`Transaction`** for the views that changed. An expanding card that jumps from its collapsed to its expanded height means that, for that view, the update had no animation, or the change was not something SwiftUI interpolates. A senior answer works through the causes in order of how often they occur. ## 1. Is the mutation inside the animated transaction? `withAnimation(_:_:)` sets its animation on "the thread's current Transaction" while its closure runs. Its closure is synchronous: the parameter is `() throws -> Result`, not `async`, so it cannot `await`. Two common shapes break this: - **A `Task` inside `withAnimation`.** The closure starts the task and returns; the task's body runs later, and any mutation after an `await` is a new update without the animation. - **A model updated from a callback.** If the expansion flag is set when data arrives, the action that called `withAnimation` has long finished. The fix is to finish the asynchronous work first and wrap only the state change: ```swift Task { let loaded = await loadDetails() withAnimation(.smooth) { details = loaded isExpanded = true } } ``` ## 2. Does an implicit modifier watch the right value and wrap the right modifier? When the card relies on `animation(_:value:)` instead, two details decide whether it fires: - **The value.** The modifier applies its animation in updates where its `Equatable` value changed. `.animation(.smooth, value: isLiked)` on a card whose height depends on `isExpanded` does nothing for the expansion. - **The placement.** A modifier wraps what is written before it. If `.animation(...)` is written before `.frame(height: isExpanded ? 240 : 80)`, the frame change sits outside the animated subtree. Move the animation modifier after the modifiers whose values change. ## 3. Is something below removing the animation? An animation travels down the hierarchy inside the transaction, and any view on the way can change it: - `.transaction { $0.animation = nil }` written on a container clears the animation for everything inside it. - `.animation(nil, value: x)` means "do not animate" whenever `x` changes, and an update that changes `x` together with the height loses the animation in that subtree. - A `withTransaction` that sets `disablesAnimations` asks views not to animate. In the SDK's inlined code for the value-less `animation(_:)` modifier, the modifier only installs its animation when that flag is false. Search the card's subtree and its ancestors for these before suspecting SwiftUI. ## 4. Is the change an interpolation at all? SwiftUI interpolates **animatable values that differ between two renders of the same view**. Two situations defeat the expected animation even with a correct transaction: 1. **Identity changed.** If the collapsed and expanded cards are two branches of an `if`, or the card's `id(_:)` changes with the state, SwiftUI removes one view and inserts another. That is a transition, not an interpolation of height; view identity and insertion transitions are covered with SwiftUI's identity rules and transitions. 2. **The changing value is not animatable.** Text content changing from one string to another is not interpolated like a frame; a content transition such as `contentTransition(.interpolate)` or `.numericText()` controls how it animates. A custom view's own stored numbers need `Animatable` conformance to interpolate, which belongs with custom interpolation. ## A diagnosis order | Symptom | Likely cause | Fix | |---|---|---| | Works for a synchronous toggle, jumps after loading | Mutation after an `await` | Await first, then `withAnimation` around the mutation | | Another state change animates, this one jumps | Modifier watches a different value | Watch the value that changes, or use `withAnimation` | | Everything inside one container jumps | `transaction` or `animation(nil, value:)` on or above it | Remove or narrow the override | | The card swaps instead of growing | Identity changed | Keep one view and vary its frame | Work through it top to bottom: find the line that mutates the state, confirm it is inside the animated closure or that a value-scoped modifier sees it, then walk the hierarchy for overrides, then question identity. Each step is a single read of the code, and in most real reports the first or second step finds the cause.
- Why can you not simply write withAnimation { await loadDetails() } to animate the result?`withAnimation`'s closure is synchronous, `() throws -> Result`, so it cannot contain `await`. Even if a `Task` is started inside it, the mutation that follows the `await` happens after `withAnimation` returned, in a later update whose transaction has no animation. Await the work first, then wrap only the state change in `withAnimation`.
- The collapsed and expanded cards are two branches of an if statement. Why does the card swap instead of growing?Each branch is a different view to SwiftUI, so changing the condition removes one and inserts the other, and that change is animated by an insertion and removal transition. There is no single frame to interpolate. Keeping one card and varying its `frame` or `lineLimit` with the state lets SwiftUI animate the height.
- Starting a Task inside withAnimation animates whatever that Task changes later.
- animation(_:value:) animates the view whenever anything changes, so the watched value hardly matters.
- An animation(_:value:) written before the frame modifier still covers that frame change.
- Two if branches showing the card are interpolated like one view.
- A jump means SwiftUI dropped the animation, so wrapping the change in DispatchQueue.main.async fixes it.