UIStackView Hide View Animation
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
UIStackView has a powerful built-in feature: when you set a subview's isHidden property to true, the stack view automatically removes it from the layout and redistributes space to the remaining views. Animating this transition produces smooth expand/collapse effects without manual constraint management. This is one of the simplest and most effective animation techniques in iOS development.
Basic Hide/Show Animation
To show:
The stack view automatically adjusts spacing and repositions the remaining arranged subviews.
Toggle with Spring Animation
Practical Example: Expandable Section
A common pattern is a header that expands/collapses content when tapped:
Combining Alpha with isHidden
For a smoother fade effect, animate both alpha and isHidden:
Animating Multiple Views
Hide or show multiple views simultaneously:
Sequential Animation
Reveal views one at a time:
UIStackView Properties That Affect Animation
These properties can also be animated:
In a UITableViewCell
Stack view animations inside table view cells require updating the table view:
Common Pitfalls
- Double-toggling isHidden: Calling
isHidden = trueon an already-hidden view, orisHidden = falseon a visible view inside an animation block can cause layout glitches. Always check the current state or use.toggle(). - Nested stack views: When hiding a view inside a nested stack view, both the inner and outer stack views animate, which can produce unexpected layout changes. Test nested configurations carefully.
- Constraint conflicts: If a hidden view has explicit height/width constraints (not just intrinsic size), the stack view may not collapse it properly. Remove or deactivate explicit size constraints when hiding.
- Auto Layout ambiguity: If your stack view's arranged subviews do not have sufficient constraints (e.g., missing intrinsic content size), the animation may produce jumps. Ensure all views have well-defined sizes.
- layoutIfNeeded placement:
layoutIfNeeded()must be called inside the animation block. Calling it outside produces an instant layout change with no animation.
Summary
- Set
isHiddenon a stack view's arranged subview insideUIView.animatefor smooth expand/collapse - Always call
stackView.layoutIfNeeded()inside the animation block - Combine
alphaanimation withisHiddenfor fade effects - Animate
spacingand other stack view properties for additional effects - For table view cells, call
beginUpdates()/endUpdates()after the animation to update row heights
Related reading
- UIStackView Is it really necessary to call both removeFromSuperView and removeArrangedSubview to remove a subview?
- UIStackView Unable to simultaneously satisfy constraints on squished hidden views
- UIStatusBarStyle not working in Swift
- UITableView - change section header color
- UITableView - scroll to the top
- UITableView Add content offset at top
- UITableView Cell selected Color?
- UITableView clear background
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.