Auto-layout interview questions

autolayout life cycle, setNeedslayout vs SetNeedsUpdateConstraints
2. Can we override these functions in UIViewController or UIView and which function is going to call first updateViewConstraint or viewWillLayoutSubviews? 3. Can we override these functions in…

Auto-layout Cycle in iOS (The Layout Cycle)

As auto-layout is integrated part of iOS applications, Let’s start with few questions to yourself -

1. When do we need to call setNeedsLayout and setNeedsUpdateConstraints.?

2. Can we override these functions in UIViewController or UIView and which function is going to call first updateViewConstraint or viewWillLayoutSubviews?

3. Can we override these functions in UIViewController or UIView and which function is going to call first updateConstraints or layoutSubviews?

Answer in a nutshell -

The Layout Cycle

  • Run Loop -> Constraints Change -> Deferred Layout Pass

Constraint changes: activating/deactivating, adding/removing views ,changing certain controls

The Deferred Layout Pass

Instead of immediately updating the affected views’ frames, Auto Layout schedules a layout pass for the near future. This deferred pass updates the layout’s constraints and then calculates the frames for all the views in the view hierarchy. During this deferred pass, not only is the given View’s Constraint updated, the Constraints for every View in the hierarchy are recalculated and updated to adjust for the new layout.

You can schedule your own deferred layout pass by calling the setNeedsLayout method or the setNeedsUpdateConstraints method.

The Deferred Layout Pass consists of two unique passes through the view hierarchy:

  • The Update Pass — In this pass, the Auto Layout Engine traverses the view hierarchy and invokes the UpdateViewConstraints method on all View Controllers and the UpdateConstraints method on all Views.
  • The Layout Pass — Again, the Auto Layout Engine traverses the view hierarchy, but this time invokes the ViewWillLayoutSubviews method on all View Controllers and the layoutSubviews method on all Views. The layoutSubviews method updates the Frame property of each subview with the rectangle calculated by the Auto Layout Engine.

The following rules should help you avoid feedback loops:

  • Do not call setNeedsUpdateConstraints inside your updateConstraints method. Calling setNeedsUpdateConstraints schedules another update pass, creating a feedback loop.
  • You must call the superclass’s implementation somewhere in your method.
  • You can safely invalidate the layout of views in your subtree; however, you must do this before you call the superclass’s implementation.
  • Don’t invalidate the layout of any views outside your subtree. This could create a feedback loop.
  • Don’t call setNeedsLayout inside layoutSubviews,Calling this method creates a feedback loop.
  • Be careful about changing constraints. You don’t want to accidentally invalidate the layout of any views outside your subtree.

setNeedsLayout()

The method setNeedsLayout for a UIView tells the system that you want it to layout and redraw that view and all of its subviews, when it is time for the update cycle. This is an asynchronous activity, because the method completes and returns immediately, but it isn’t until some later time that the layout and redraw actually happens, and you don’t know when that update cycle will be.

layoutIfNeeded()

In contrast, the method layoutIfNeeded is a synchronous call that tells the system you want a layout and redraw of a view and its subviews, and you want it done immediately without waiting for the update cycle. When the call to this method is complete, the layout has already been adjusted and drawn based on all changes that had been noted prior to the method call.

layoutSubviews()

The default implementation uses any constraints you have set to determine the size and position of any subviews.

Subclasses can override this method as needed to perform more precise layout of their subviews. You should override this method only if the autoresizing and constraint-based behaviors of the subviews do not offer the behavior you want. You can use your implementation to set the frame rectangles of your subviews directly.

Hope this article is useful for people looking to apply autolayout programmatically, Please ❤️ to recommend this post to others 😊. Let me know your feedback. :)