In SwiftUI, a card's height animates through withAnimation, but an image inside it should snap to its new size; how do you exempt the image?
answer
- the update's context travels down
- transform it on one subtree
- set the animation to nil
- leaf views, not containers
- disablesAnimations for the whole change
basics
~10 sApply transaction(:) to the image and set the transaction's animation to nil, so that subtree changes without animation while the card still animates; on iOS 17, transaction(value::) or transaction(_:body:) narrow the override further.
solid answer
~40 sThe animation travels down the hierarchy inside the update's `Transaction`, so I change it where the image is: `.transaction { $0.animation = nil }` on the image makes everything inside that modifier update without animation while the card's own frame keeps the `withAnimation` curve. If only one modifier should snap, iOS 17's `transaction(_:body:)` applies the override to the modifiers inside its closure, and `transaction(value:_:)` applies it only when a given value changes. Apple's documentation recommends putting `transaction(_:)` on leaf views such as `Image` or `Button` rather than containers. For a change that should not animate anywhere, I run it inside `withTransaction` with `disablesAnimations` set, which asks views, including implicit animation modifiers, not to animate it.
code
swift · 20 linesstruct TrailCard: View {
@State private var isExpanded = false
var body: some View {
VStack(alignment: .leading, spacing: 12) {
Image(systemName: "photo")
.resizable()
.scaledToFit()
.frame(height: isExpanded ? 220 : 80)
// Snaps on this change only; other changes still animate.
.transaction(value: isExpanded) { $0.animation = nil }
Text("Eight kilometres, mostly shaded, one steep climb near the end.")
.lineLimit(isExpanded ? nil : 1)
}
.padding()
.onTapGesture {
withAnimation(.snappy) { isExpanded.toggle() }
}
}
}go deeper
Recall that SwiftUI passes animations down the view tree and that a view can turn the animation off for itself.
Explain Transaction's animation and disablesAnimations properties and how transaction(_:) changes them for one subtree.
Choose the narrowest tool, transaction(value::) or transaction(:body:), place it on a leaf view after the changing modifier, and know when a whole change needs disablesAnimations.
Be ready to set a review rule that bans container-wide transaction overrides and explain how they cause hard-to-trace jumps across a screen.
## Transactions carry the animation Every state change in SwiftUI is processed in an update, and the context of that update is a **`Transaction`**. Apple's documentation says to "use a transaction to pass an animation between views in a view hierarchy", and that the root transaction for a change comes from the binding that changed plus any values set by `withTransaction(_:_:)` or `withAnimation(_:_:)`. Its main members for this question are: - **`animation`**: the animation, if any, for this state change. - **`disablesAnimations`**: "a Boolean value that indicates whether views should disable animations". - **`isContinuous`**: whether the change came from an action that produces a sequence of values, such as a drag. - **`addAnimationCompletion(criteria:_:)`** (iOS 17): a completion to run when the transaction's animations finish. Because the transaction travels down the hierarchy, any view can transform it for its own subtree. That is how you exempt a part of the screen from an animation started higher up. ## Removing the animation from one subtree In the expanding card, `withAnimation(.snappy) { isExpanded.toggle() }` animates the card's height. The image inside it also changes size, and stretching it with the card looks wrong. The fix sits on the image: ```swift Image("trail") .resizable() .scaledToFill() .frame(height: isExpanded ? 220 : 80) .clipped() .transaction { $0.animation = nil } ``` `transaction(_:)` "applies the given transaction mutation function to all animations used within the view". Setting `animation` to `nil` there means the image and the modifiers written before the `transaction` call update without animation, while the card around it still animates with `.snappy`. The same closure can also adjust rather than remove the animation; Apple's sample uses `view.animation = view.animation?.delay(2.0).speed(2)` to delay one view and replaces another's with a spring. ## Narrowing the override on iOS 17 Two iOS 17 forms keep the override from reaching further than intended: | API | Applies the transform | |---|---| | `transaction(_:)` | To every transaction in this subtree, on every update | | `transaction(value:_:)` | Only when the given `Equatable` value changes | | `transaction(_:body:)` | Only to the modifiers written inside `body` | | `animation(_:body:)` | Sets an animation for the modifiers inside `body` | With `transaction(value: isExpanded) { $0.animation = nil }` the image snaps when the card expands but still animates other changes, for example a favourite badge that pops. With `transaction(_:body:)` the image can keep animating its opacity while its frame snaps, because only the modifiers inside the closure see the override. Because these date from iOS 17, which is the baseline for most production apps, they are usually available. ## Disabling animations for a whole change Sometimes the change itself should not animate anywhere, for example resetting a form after it was submitted while some views carry `animation(_:value:)` modifiers. Not calling `withAnimation` is not enough, because those implicit modifiers would still animate when their values change. A transaction with `disablesAnimations` set asks views not to: ```swift var transaction = Transaction() transaction.disablesAnimations = true withTransaction(transaction) { draft = Draft() } ``` The key-path overload `withTransaction(\.disablesAnimations, true) { ... }` does the same in one line. The SDK's inlined code for the value-less `animation(_:)` modifier shows the contract: the modifier installs its animation only when `disablesAnimations` is false. Treat the flag as a request that animation modifiers honour, and verify the result on screen for the modifiers you depend on. ## Where to put the modifier Apple's documentation for `transaction(_:)` advises using it "on leaf views such as Image or Button rather than container views such as VStack or HStack", because the transformation applies to all child views and calling it on a container "can lead to unbounded scope of execution depending on the depth of the view hierarchy". Practical rules: 1. Put the override on the smallest view that should behave differently. 2. Prefer `transaction(value:_:)` or `transaction(_:body:)` when only one value or one modifier is involved. 3. Never clear the animation on a screen's root to fix one view; every later animation in that screen will mysteriously jump. ## Pitfalls - Writing `.transaction { $0.animation = nil }` before the `frame` modifier: the frame then sits outside the overridden subtree and still animates. - Using `.animation(nil)` without a value: that is the value-less form deprecated in iOS 15; use a transaction or `.animation(nil, value:)`. - Expecting `disablesAnimations` to cancel an animation already running; it describes the update it is part of.
- Why does Apple recommend applying transaction(_:) to leaf views rather than containers?The transformation applies to every child view inside the modifier. On a `VStack` or `HStack` high in the hierarchy it runs for a whole subtree, which the documentation warns "can lead to unbounded scope of execution depending on the depth of the view hierarchy", and it silently changes animations for views you did not mean to touch. On an `Image` or `Button` the effect is small and obvious.
- How would you reset a form without any animation when several fields carry animation(_:value:) modifiers?Run the reset inside a transaction whose `disablesAnimations` is true: `withTransaction(\.disablesAnimations, true) { draft = Draft() }`. Skipping `withAnimation` alone is not enough, because the value-scoped modifiers would still animate when their values change. The flag asks views not to animate this update; check the screen to confirm each modifier honours it.
- To stop one view animating you must remove withAnimation from the action.
- transaction(_:) belongs on the root container so it covers everything.
- Not calling withAnimation guarantees no implicit animation modifier will animate the change.
- The value-less animation(nil) is the modern way to exempt a view.
- The transaction modifier's position in the chain does not matter.