A review that tends to reinforce and solidify memories.


After a Runloop has processed the event, it finally enters the Update cycle:

  1. Update the constraints
  2. Layout: The engine calculates the frame and layout of views and subviews
  3. Display: Redraw view

Step1: Update constraint

updateConstraints()

Similar to layoutSubviews.

Only the constraints that must be updated should be implemented in this method; static constraints should be specified in the Interface Builder, view initializer, or viewDidLoad() method.

Instead of calling directly, you should call the triggering method and let the system handle it

Automatic tag constraints change and the next cycle triggers updateConstraints’ behavior:

  • Set/unbind
  • Change the constraint priority
  • Remove the view from the View Hierarchy

setNeedUpdateConstraints()

Similar to the mechanism used in setNeedsLayout, manual markup changes and the next cycle calls updateConstraints for an update

updateConstraintsIfNeeded()

A mechanism similar to layoutIfNeeded that checks to see if the constraints on the view have changed and calls updateConstraints immediately if they have

invalidateIntrinsicContentSize()

Setting a flag indicates that the view’s intrinsicContentSize has expired and needs to be recalculated in the next layout phase.

View’s intrinsicContentSize: The natural dimensions of the view according to its own content, usually defined by the constraints of the containing elements, and also customizable

Step2: Layout view

layoutSubviews()

Handling repositioning and resizing of a view and all its children invokes the child’s layoutSubviews, which is expensive

Instead of calling directly, you should call the triggering method and let the system handle it

Automatically mark the view’s layout changes and trigger the behavior of layoutSubviews in the next cycle:

  • Change the bounds of a view (not just its position) by setFrame
  • add subview
  • UIScrollView scrolling (layoutSubviews are called on UIScrollView and its parent view)
  • Rotate device: Call layoutSubviews of the current controller self.view
  • Update the constraints of the view

setNeedsLayout()

Manually flag that the layout of the view has changed and call layoutSubviews for updates the next cycle

layoutIfNeeded()

Check to see if the layout of the view has been marked for change and call layoutSubviews immediately if it has

Step3: draw

drawRect()

Similar to layoutSubviews, but does not trigger calls to subview methods

Instead of calling directly, you should call the triggering method and let the system handle it

The automatic tag view display changes and the drawRect behavior is triggered at the next cycle:

  • Change the bounds of the view

setNeedsDisplayInRect()

Similar to the setNeedsLayout mechanism, the manual flag changes and the drawRect update is called in the next cycle

The display method does not have a method like layoutIfNeeded