Auto Layout


I’ve always taken for granted that whenever I make changes to constraints on a view, I’d expect the changes to get applied to the view. This chapter will look into when & how this happens. Let’s demystify auto layout.


The Layout Cycle

The application manages the layout cycle on its main run loop as follows:

  1. Handle events on the main dispatch queue
  2. Check for constraints changes
  3. If constraints have changed, schedule a deferred layout pass

Image from Presentation Slides, WWDC 2015, Session 219


Registering Constraint Changes

  1. Constraint changes are translated into mathematical expressions in the layout engine.
  2. Based on the constraint changes, the layout engine re-computes the dimensions & coordinates of the views & controls that need to be updated.
  3. These views get notified, and calls superview.setNeedsLayout(), causing the deferred layout pass to schedule.

Deferred Layout Pass

The deferred layout pass consists of 2 passes - the update pass & the layout pass.

The Update Pass:

The intention of the update pass is to register constraint changes for current layout pass. The layout engine traverses the view hierarchy, calling:

The Layout Pass:

Finally, the layout pass reposition affected views by assigning new dimensions & coordinates. In this pass, the layout engine traverses the view hierarchy, calling:


PreEmpting the Deferred Layout Pass

What can I do if I want to effect my layout changes right away instead of waiting until the next layout pass?


Wrapping Up

Constraint Updates:

Layout Updates: (ie, frame changes, view added, view resizes)

Do Nots: