Hero swiftui
Implement Hero shared-element transitions in SwiftUI apps that use a UIKit navigation wrapper (iOS). Use when the user wants views, cards, images, search bars, or any element to morph smoothly between two screens on push/pop. Triggered by keywords like "Hero animation", "shared element transition", "morph between screens", "matched geometry across navigation", "hero.id", "HeroTransitions", or references to the Hero iOS library.From its SKILL.md
npx -y skills add trmquang93/ios-dev-kit --skill hero-swiftuiAssembled from the repository path, not quoted from the project. Check it against their README if it does not work.
One thing to look at
- 0 stars0 stars. Stars are a popularity signal and not a quality one, but at this level it is likely that nobody has read this closely except its author, and you would be relying on your own review.
SKILL.md
7.5 KB, as published. Nobody here has run it
Hero Shared-Element Transitions in SwiftUI
Hero (https://github.com/HeroTransitions/Hero) is a UIKit library that morphs matching-ID views between two view controllers during navigation. It works at the UIView layer. In a SwiftUI app, you must bridge Hero to SwiftUI via UIViewRepresentable — you cannot apply hero.id to a SwiftUI View directly.
When to reach for this skill
- Two screens show the "same" visual element at different positions/sizes, and you want it to fly smoothly between them on push and pop (search bars, card-to-detail, thumbnail-to-hero-image, tag pills, avatars).
- Works in any SwiftUI app. If the project is using pure
NavigationStack, you first need a UIKit-backed navigation wrapper — a complete drop-in one ships with this skill at navigation-wrapper-notes.md. If the project already has one (check forUIViewControllerRepresentablewrappingUINavigationControllerwithhero.isEnabled = true), skip straight toHeroContainerusage.
Mental model — the single most common mistake
Hero only morphs the exact UIView that carries hero.id. If you set the ID on a background layer only, the rest of the SwiftUI content (icons, labels, text fields) just fades in place because those SwiftUI views are rendered inside the source/destination UIHostingController and are not part of the Hero pair. The background "flies to the wrong spot" while siblings fade, which looks broken.
Fix: wrap the entire visual unit you want to morph (icon + text + background + whatever) inside a single UIView that carries hero.id. Hero snapshots and morphs the whole unit together.
The bridging primitive — HeroContainer
Create a reusable generic UIViewRepresentable that:
- Creates a UIView with
hero.idandhero.modifiersset on it. - Embeds a
UIHostingControllerwith arbitrary SwiftUI content. - Pins the hosting view to its edges with Auto Layout.
- Implements
sizeThatFits(_:uiView:context:)to forward size to SwiftUI layout. Without this the view balloons to fill all available space, destroying surrounding layout. - Attaches the hosting controller to the nearest parent VC on
didMoveToWindowso lifecycle/constraints behave correctly.
See HeroContainer.swift for the full reference implementation. Copy it into Views/Components/ (or similar) in any project.
Using HeroContainer
Apply the same heroID to the matching element on both source and destination screens:
// Source (e.g. HomeSearchBar)
HeroContainer(
heroID: "searchBar",
heroModifiers: [.spring(stiffness: 200, damping: 22)]
) {
HStack {
Image(systemName: "magnifyingglass")
Text("Search for files")
Spacer()
}
.padding(.horizontal, 16)
.padding(.vertical, 14)
.background(Capsule().fill(.gray.opacity(0.1)))
}
.onTapGesture { router.navigate(to: .search) }
// Destination (e.g. SearchView)
HStack {
HeroContainer(
heroID: "searchBar",
heroModifiers: [.spring(stiffness: 200, damping: 22)]
) {
HStack {
Image(systemName: "magnifyingglass")
TextField("Search for files", text: $searchText)
}
.padding(.horizontal, 12)
.padding(.vertical, 10)
.background(Capsule().fill(.gray.opacity(0.1)))
}
Button("Cancel") { router.goBack() }
}
Rules:
- Put outer padding (page margins, layout spacers) OUTSIDE
HeroContainer. Hero measures the source/destination frame from the container's own frame. Keeping padding outside makes the morph travel between the two visible pill frames, not the outer page rectangles. - Put interaction gestures (
onTapGesture, etc.) OUTSIDE the container unless the gesture is part of the morphed element itself. - Source and destination hierarchies can differ (Text vs. TextField, extra sibling Button, etc.). Hero snapshots each side — they just need matching
heroID. - The pill/capsule/background visual should live inside the HeroContainer's content so it's part of the snapshot.
Navigation wrapper requirements
Hero requires UIKit navigation. At minimum:
// Root
let nav = UINavigationController(rootViewController: rootHostingController)
nav.hero.isEnabled = true
nav.hero.navigationAnimationType = .auto // or .fade, .push, etc.
rootHostingController.hero.isEnabled = true
// Every pushed controller
pushedHostingController.hero.isEnabled = true
nav.pushViewController(pushedHostingController, animated: true)
Per-route override just before pushing:
nav.hero.navigationAnimationType = .fade // unpaired content fades; paired pairs still morph
nav.pushViewController(hostingController, animated: true)
.fade,.auto,.push,.cover, etc. control how unpaired content animates. Pairedhero.idviews morph regardless.- If you forget
hero.isEnabled = trueon either the nav controller OR a hosting controller, Hero is silently disabled for that transition.
Hero modifiers worth knowing
.spring(stiffness:damping:)— physics-based morph curve. Good default..duration(_:),.delay(_:),.curve(_:)— manual timing..useNoSnapshot— Hero moves the real UIView instead of a snapshot. Needed only for views with live interactive state that break when snapshotted (rare). For aHeroContainerwrapping SwiftUI content, leave this off — the snapshot crossfade handles differing source/destination SwiftUI hierarchies gracefully..zPosition(_:)— raise a pair above others during the transition..cascade— for lists, offset children's animations by a delay.
Debugging checklist when it doesn't work
- Is
hero.isEnabled = trueon the nav controller AND both hosting controllers? - Do BOTH the source and destination have a
HeroContainerwith the sameheroID? - Is the
heroIDunique per transition (no duplicate IDs on screen)? - Is the element actually on-screen at push time? (Off-screen source means nothing to morph from.)
- Did you forget
sizeThatFitsinHeroContainer? Symptom: the element expands to fill all available space and surrounding layout breaks. - Is
.useNoSnapshotset somewhere it shouldn't be? It can produce weird results with SwiftUI hierarchies that differ between source and destination. - Are the two
HeroContainerframes at plausibly similar points on screen? Huge jumps look correct but can feel broken; consider matching outer padding between screens.
Things that are NOT solved by Hero
- Same-screen matched geometry (element moves between positions on one view) → use SwiftUI's
matchedGeometryEffectinstead. - Sheet / modal presentations that don't go through the UIKit navigation controller → Hero can do modal too (
modalAnimationTypeon the presented VC) but requires its own wiring. - TabView cross-tab transitions → no built-in support; would require custom work.
Files in this skill
- HeroContainer.swift — copy-paste-ready bridging primitive.
- navigation-wrapper-notes.md — minimal UIKit nav wrapper snippet for SwiftUI apps that don't already have one.