AutoLayout with hidden UIViews?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
A hidden UIView does not automatically disappear from Auto Layout. Setting isHidden = true stops the view from drawing and usually stops interaction, but its constraints still exist unless you change them, remove the view, or use a container that understands arranged subviews specially.
What isHidden Actually Does
isHidden affects visibility, not layout participation. If a view has a fixed height constraint, leading and trailing constraints, and spacing constraints to neighboring views, those constraints remain active after the view is hidden.
That is why developers often hide a view and then wonder why there is still empty space where the view used to be. Auto Layout is honoring the constraints exactly as requested.
A Common Fix: Toggle Constraints
One direct solution is to activate one set of constraints when the view is visible and another set when it is hidden. A frequent pattern is to keep a height constraint outlet and set it to 0 when hiding the view.
This works well when the view has a predictable collapsed size and you control the relevant constraints explicitly.
Better for Vertical Stacks: UIStackView
If the layout is a simple stack of views, UIStackView is often the cleanest option. A hidden arranged subview is automatically removed from the stack’s layout calculation, which is much closer to what developers expect.
This is one of the reasons UIStackView is so useful for dynamic forms and settings screens.
Remove the View When It Should Not Affect Layout
Sometimes the hidden view should not participate in layout at all. In that case, removing it from the hierarchy is more honest than merely hiding it.
That approach is stronger than isHidden, but it also means you must restore the view later if it needs to come back.
Constraint Priority Patterns
Another useful technique is to create competing constraints with different priorities. For example, one constraint can preserve the normal spacing when the view is visible, while another constraint collapses the gap when the view is hidden. Then you raise and lower priorities as state changes.
This pattern is more flexible than a zero-height constraint when the layout rules are complex, but it also becomes harder to debug. Use it when a simple stack or fixed-height toggle is not enough.
Animation Behavior
Hiding a view alone is not a layout animation. If the layout changes because constraints changed, call layoutIfNeeded() inside an animation block on the parent view. That animates the movement of neighboring views smoothly instead of snapping them instantly.
For UIKit code, the rule of thumb is simple: change the constraints first, then animate the layout pass.
Common Pitfalls
The biggest pitfall is assuming isHidden removes constraints. It does not. If the view occupies space after hiding, the constraints are doing exactly what they were told to do.
Another mistake is setting the height to zero but leaving conflicting top, bottom, or spacing constraints untouched. Auto Layout may then produce warnings because the system still has incompatible requirements.
Developers also overuse manual constraint toggling when UIStackView would solve the problem with much less code. If the interface is stack-like, lean on arranged subviews rather than building a custom collapse system.
Finally, do not forget accessibility and interaction flow. A hidden view is not visible, but if your logic depends on the view existing for navigation or state restoration, removing it completely may change behavior in ways you did not intend.
Summary
- '
isHiddenaffects visibility, not constraint activity.' - Hidden views still occupy layout space unless you change constraints, remove the view, or use
UIStackViewarranged-subview behavior. - A zero-height constraint is a simple and effective collapse pattern.
- '
UIStackViewis often the best option for dynamic vertical or horizontal groups.' - When debugging hidden-view layout issues, inspect the active constraints first rather than the visibility flag alone.

