UIStackView
Auto Layout
iOS Development
Constraint Error
Hidden Views

UIStackView Unable to simultaneously satisfy constraints on squished hidden views

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

This warning usually appears when a view inside a UIStackView is hidden, but some of its own constraints still insist that it keep a nonzero size. UIStackView tries to collapse hidden arranged subviews out of the layout, while your explicit constraints try to preserve space. The Auto Layout engine then has to break something, and you see the familiar unsatisfied-constraints log.

Understand What UIStackView Does With Hidden Arranged Subviews

A stack view treats arrangedSubviews differently from ordinary subviews. When an arranged subview becomes hidden, the stack view generally stops allocating normal layout space for it. The view still exists in the hierarchy, but the stack wants that area to collapse.

If the hidden view has a required height, width, or internal edge constraint that still demands room, the stack view and the hidden view are now pushing in opposite directions.

A Typical Failing Setup

This simplified example shows the pattern.

swift
1let stackView = UIStackView()
2stackView.axis = .vertical
3stackView.spacing = 8
4
5let detailsView = UIView()
6let heightConstraint = detailsView.heightAnchor.constraint(equalToConstant: 80)
7heightConstraint.isActive = true
8
9stackView.addArrangedSubview(detailsView)
10detailsView.isHidden = true

The stack view wants detailsView to collapse, but the explicit required height constraint says the view must remain 80 points tall. The warning is the layout engine telling you those rules cannot both win.

Deactivate Size Constraints When Hiding the View

The cleanest fix is often to toggle the conflicting constraint together with visibility.

swift
1final class DetailsController: UIViewController {
2    private let stackView = UIStackView()
3    private let detailsView = UIView()
4    private lazy var detailsHeight = detailsView.heightAnchor.constraint(equalToConstant: 80)
5
6    override func viewDidLoad() {
7        super.viewDidLoad()
8        stackView.axis = .vertical
9        stackView.spacing = 8
10        stackView.translatesAutoresizingMaskIntoConstraints = false
11        detailsView.backgroundColor = .systemGray5
12        stackView.addArrangedSubview(detailsView)
13        detailsHeight.isActive = true
14    }
15
16    func setDetailsVisible(_ visible: Bool) {
17        detailsHeight.isActive = visible
18        detailsView.isHidden = !visible
19        view.layoutIfNeeded()
20    }
21}

This keeps the layout rules consistent. When the view is hidden, the stack is free to collapse it.

Remove the Arranged Subview When the View Is Truly Optional

If the view is not just temporarily hidden but structurally absent, remove it from the arranged subviews instead of keeping it hidden forever.

swift
stackView.removeArrangedSubview(detailsView)
detailsView.removeFromSuperview()

That removes the conflict entirely and is often the best choice for optional sections such as error banners, advanced settings panels, or empty-state content.

Be Careful With Nested Constraints

Sometimes the direct height constraint is not the problem. The warning can come from constraints inside the hidden view, such as top and bottom anchors, image aspect-ratio constraints, or a label container with required padding.

In those cases, hiding the outer view is not enough because the internal required constraints still describe a layout the stack can no longer satisfy. A wrapper view can help.

Use a Wrapper View for Complex Content

If a content block has many internal rules, place it inside a container view and add the container as the arranged subview. Then hide or remove the container instead of the inner content.

This gives the stack view one object to collapse while the inner layout remains self-contained when visible.

Lower Priorities When Appropriate

Not every size rule needs to be required. If a height or spacing constraint is only a preference, lower its priority so Auto Layout can relax it during collapse.

swift
let preferredHeight = detailsView.heightAnchor.constraint(equalToConstant: 80)
preferredHeight.priority = .defaultHigh
preferredHeight.isActive = true

Do this only when the rule is genuinely negotiable. Lowering priorities to silence warnings without understanding the layout usually creates a fragile interface.

Debug the Actual Broken Constraint

When Xcode prints a long constraint log, focus on the constraints attached to the hidden arranged subview and any explicit dimension constraints. That is almost always where the contradiction lives.

A useful discipline is to ask one concrete question: if this view is hidden, which constraints still require it to occupy space. Once you identify those, the fix is usually straightforward.

Common Pitfalls

  • Hiding an arranged subview but leaving a required height or width constraint active.
  • Looking only at the stack view and missing constraints inside the hidden view's subtree.
  • Lowering priorities blindly instead of removing the actual contradiction.
  • Keeping permanently optional sections hidden instead of removing them from arranged subviews.
  • Assuming isHidden automatically makes every related constraint harmless.

Summary

  • 'UIStackView wants hidden arranged subviews to collapse out of layout space.'
  • Warnings appear when other required constraints still force the hidden view to keep a size.
  • The usual fix is to deactivate those size constraints when hiding the view.
  • For truly optional content, remove the arranged subview completely.
  • Wrapper views and careful priority choices help with more complex nested layouts.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Browse interview questions

All Rights Reserved.