UIView Hide/Show with animation
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Showing and hiding a UIView with animation is easy to start and easy to get subtly wrong. Common bugs include hidden views intercepting touches, views that never become visible again, and layout jumps during state changes. A robust solution coordinates alpha, isHidden, and Auto Layout updates in a predictable sequence.
Core Visibility Principles
isHidden removes drawing and interaction immediately. alpha animates visual opacity but does not remove the view from layout. Good transitions usually combine both.
Recommended behavior:
- For show: unhide first, set low alpha, animate to full alpha.
- For hide: animate alpha to zero, then set hidden in completion block.
This helper is reusable, keeps behavior consistent, and avoids timing mistakes repeated across screens.
Animate Layout and Visibility Together
If the view also moves by constraint changes, animate layoutIfNeeded in the same block.
If layout changes and alpha changes are split into unrelated animations, transitions feel broken.
Interaction and Accessibility Considerations
A view with alpha = 0 but isHidden = false can still block touches. For hidden states, always set isHidden = true eventually. For modal-style views, also consider isUserInteractionEnabled during transition phases.
Respect reduced-motion settings for users who prefer minimal animation.
Using this helper in your show/hide calls keeps interaction inclusive and predictable.
Dealing With Repeated Toggle Calls
Rapid taps can start overlapping animations. Use .beginFromCurrentState and keep one source of truth for visibility.
This prevents inconsistent intermediate states during fast user interaction.
Testing Checklist
Use a practical checklist to validate behavior:
- Hidden to visible transition keeps final alpha at
1. - Visible to hidden transition sets
isHidden = trueafter fade. - Rapid repeated taps do not leave half-visible states.
- Constraint animation remains in sync with fade.
- Reduced-motion mode still yields correct final state.
Visual bugs in animation often come from one missed state flag, so checklist testing pays off.
Common Pitfalls
A common pitfall is setting isHidden to true before fade-out begins, which makes animation invisible. Another issue is forgetting to restore alpha to 1 before showing again, leaving the view present but still transparent. Teams also animate on background threads in indirect async callbacks, which can cause undefined UI behavior. In layout-heavy screens, not animating layoutIfNeeded with the same timing as alpha leads to jumpy movement. Finally, repeated toggles without state control can produce stuck views that are visible but non-interactive.
Summary
- Coordinate
alphaandisHiddeninstead of toggling only one property. - Unhide before fade-in and hide only after fade-out completion.
- Animate constraints and opacity together for smooth transitions.
- Handle rapid toggles with explicit state guards.
- Respect reduced-motion settings while preserving final visibility state.
Related reading
- UIView Infinite 360 degree rotation animation?
- UIView shake animation
- UIView touch event in controller
- UIView touch event in controller
- UIView with rounded corners and drop shadow?
- UIViewContentModeScaleAspectFill not clipping
- UIViewController viewDidLoad vs. viewWillAppear What is the proper division of labor?
- UIView's border color in Interface builder doesn't work?
.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.