← All topics/UIKit & SwiftUI internals

Practical interview questions

Scenario-style prompts with sample answer outlines. Focus is on how you would design and reason in real codebases.

Question 4

How Auto Layout works

At a high level, how does Auto Layout work? What are the major gotchas?

Answer outline

Auto layout allows you to describe relationships between views (edges, spacing, sizes). With each layout pass the engine solves those linear constraints and assigns frames.

Intrinsic content size: Many controls (UILabel, UIImageView, buttons) report a preferred width/height. The solver combines that with your constraints so labels, images, and buttons can size themselves without every dimension being hardcoded.

Priorities: Content hugging resists growing beyond intrinsic size; compression resistance resists shrinking below intrinsic size. Priorities tell Auto Layout which constraints or intrinsic sizes should yield when space is tight or extra space exists.

Gotcha — conflicts: Constraints that cannot all be true (duplicate required pins or translatesAutoresizingMaskIntoConstraints left on while also setting constraints). Gotcha — ambiguity: Not enough constraints (or zero intrinsic size) so multiple layouts are valid.

Gotcha — churn: Calling layoutIfNeeded() everywhere instead of fixing constraints; updateConstraints loops; ignoring safe area / rotation / keyboard layout guides when available space changes. Stack views help with rows and columns, but children still need enough information to size and position predictably.

Principles

  • Think relationships + intrinsic size + priorities + enough constraints.
  • Unsatisfiableambiguous: different errors, different fixes (remove/relax vs add constraints).

Follow-up angles

  • Self-sizing cells: A full vertical constraint chain on the contentView so height can be computed.