UIView life cycle using Auto Layout

Tram Ho

Auto Layout is an extremely important array of knowledge for an iOS developer. Understanding the life cycle of Auto Layout is essential to save development time and avoid application interface errors. This article will provide knowledge about the life cycle of any UIView with Auto Layout to be displayed on the screen.

Auto Layout life cycle overview

Any UIView Auto Layout set up will have to go through the following 3 steps when it is initialized: Update , Layout and Render (Figure 1). These steps do not necessarily have to be done in a one-way sequence, they can recall the steps in front or run from the beginning of the entire cycle.

Figure 1 – UIView Life Cycle with Auto Layout (Source: https://www.vadimbulavin.com/view-auto-layout-life-cycle/ )

Step Update

This step will perform a frame calculation for UIView based on constraints. The system will browse view hierarchy from top to bottom (from superview down to subviews) and call updateConstraints() for each view.

This process will run automatically, but sometimes we need to enable it intentionally. For example, we need to recalculate constraints immediately when an event changes the state of an application.

The setNeedsUpdateConstraints() disables current constraints and schedules updates to recalculate at the next cycle. The updateConstraintsIfNeeded() activates updateConstraints() immediately if constraints have been previously disabled.

Step Layout

In this step, the frames of each view will be updated with the parameters calculated in the Update step. It takes place from bottom to top according to the view hierarchy (from subviews up to superview) and calls layoutSubviews() for each view.

layoutSubviews() is the most common override method in the entire Auto Layout lifecycle. We override this method when:

  • There are not enough constraints to describe the layout of the view.
  • Frames are calculated from code.

There are two additional methods in this step: setNeedsLayout() disables the current layout and layout layoutIfNeeded() activates layoutSubviews() immediately if the layout has been disabled before.

When overriding layoutSubviews() :

  • Definitely called super.layoutSubviews() .
  • Do not call setNeedsLayout() or setNeedsUpdateConstraints() , otherwise an infinite loop will appear.
  • Do not change the constraints of views that are outside the current hierarchy.
  • Be careful when changing constraints of views in the current hierarchy, there may be infinite loops.

Rendering step

This step is responsible for displaying the views on the screen. Whether or not the view uses Auto Layout, this step is always done. UIView will transfer this display to CALayer . All changes such as changing the background color, adding subviews, … are drawn on the screen automatically. The main method of this step is drawRect() .

On the UIViewController

In the Auto Layout life cycle, in the Update and Layout steps, UIViewController has the same methods as UIView :

  • Step Update : updateViewConstraints()
  • Step Layout : viewWillLayoutSubviews() / viewDidLayoutSubviews()

The viewDidLayoutSubviews() is the most important. It is called to notify the view controller that its view has completed the Layout step (its bound has changed). This will be the time to change a view after it has finished subviews, but before it is displayed on the screen.

Intrinsic Content Size

Intrinsic Content Size is the natural size of a view based on its content (for example, the intrinsic content size of an image is its original size).

There are two tips to simplify composition and reduce the number of constraints:

  • Override the intrinsicContentSize property of a view to return the appropriate size for that view.
  • If the view has only intrinsic size for one dimension, still override the intrinsicContentSize property and return UIViewNoIntrinsicMetric for an unknown dimension.

References

https://www.vadimbulavin.com/view-auto-layout-life-cycle/

Share the news now

Source : Viblo