Skip to content

ivanopcode/devnote-swiftui--transition-removal-animation-bug

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 

Repository files navigation

Technical Note: SwiftUI Transition Removal Bug

The Issue

When conditionally removing a view from a ZStack (e.g., if isVisible { MyView() }), standard transitions like .move or .opacity often fail. The view disappears instantly ("pops" out) instead of animating out.

The Root Cause

SwiftUI manages transitions by creating a temporary "ghost" view during the removal frame. In complex hierarchies (especially ZStacks), if the z-ordering is implicit, the layout engine may lose track of the ghost view's layer context or render it behind other opaque views immediately upon state change.

The Solution

Apply an explicit, stable .zIndex to the view being removed.

if isVisible {
    SlidingPanel()
        .transition(.move(edge: .top))
        .zIndex(100) // Force stable layering
}

Why It Works

  1. Layer Identity: The explicit .zIndex tells SwiftUI this view occupies a specific rendering plane.
  2. Persistence: During the removal transition, even though the view is removed from the logical hierarchy, the rendering engine respects the z-index for the visual "ghost" view, ensuring it remains on top of siblings and visible for the duration of the animation.

About

Technical Note: SwiftUI Transition Removal Bug

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published