9.8 · Margins, stack views, guides, constraint ownership, the layout cycle and measuring
Most Auto Layout bugs are not solver bugs. They are misunderstandings of margins, stack-view rules, who owns a constraint, or when layout runs.
Mental model. Constraints are data held by views; layout is a scheduled pass. Know where the data lives and when the pass runs, and most surprises disappear.
Containers and guides
Margins, stack views and layout guides.
- What are layout margins, how do they differ from the safe area, and what changes in a right-to-left language?
Every UIView has layout margins: an inset (default 8 points on all sides for a plain view) that says how much breathing room content should keep from the… - What rules does UIStackView follow: hidden views, distribution, who stretches, and what does removeArrangedSubview not do?
A UIStackView lays out its arranged subviews along an axis with spacing, alignment (across the axis) and distribution (along it). It generates constraints for… - What is a UILayoutGuide, and when is it better than an invisible spacer view?
A UILayoutGuide is a rectangle that takes part in Auto Layout but is not a view: it has anchors (leadingAnchor, widthAnchor and so on) and a resolved…
Constraints over time
Ownership, the update cycle, measuring, and the older autoresizing system.
- Who owns a constraint, what can you change after activating it, and why is the multiplier read-only?
An active constraint is stored on a view: the nearest common ancestor of the items it connects (a width constraint on a single view lives on that view itself… - In what order do updateConstraints, layoutSubviews and the layout calls run, and how do setNeedsLayout and layoutIfNeeded coalesce?
Auto Layout has two passes, both run by the system at the end of the run-loop turn. The constraint pass calls updateConstraints() bottom-up, only on views that… - How do you measure a view or a wrapped label: systemLayoutSizeFitting, sizeThatFits and preferredMaxLayoutWidth?
Auto Layout can tell you the size a view would take: systemLayoutSizeFitting(_:withHorizontalFittingPriority:verticalFittingPriority:) solves the view’s… - How do autoresizing masks work, how does the extra space get shared, and how do they relate to Auto Layout?
Before Auto Layout, a view positioned itself relative to its superview with an autoresizing mask: when the superview’s size changes, each of the child’s six…
More in Layout, rendering & animation
- 9.1 · Auto Layout, intrinsic size, and when layoutSubviews and draw run
- 9.2 · From layout to the GPU, and the two layer trees
- 9.3 · Offscreen passes, rasterization, blending, shadows and masks
- 9.4 · Implicit and explicit animation, UIView animation, and animators
- 9.5 · Core Graphics, UIBezierPath and draw(_:)
- 9.6 · TextKit 1 and 2, and Text in SwiftUI
- 9.7 · Frame budgets, ProMotion, hitches and hangs
- 9.9 · Coordinate conversion, 3D transforms and layer contents
- 9.10 · How UIView animation really works, springs, timing and display links
- 9.11 · Transform order, fill rules, text measurement, text containers and bitmaps
- 9.12 · Rectangle containment and moving a point between coordinate spaces