Gagan Vishal

What is the difference between setNeedsLayout and layoutIfNeeded

setNeedsLayout: This is an asynchronous activity, the method completes and returns immediately. setNeedsLayout for a UIView tells the system that you want it to layout and redraw that view and all of its subviews in future.

layoutIfNeeded: This is a synchronous activity. Call 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.

Also, You never need to call any of these methods:

  • setNeedsUpdateConstraints()
  • updateConstraintsIfNeeded()
  • updateConstraints()
  • updateViewConstraints()

When we call setNeedsLayout makes sure a future call to layoutIfNeeded calls layoutSubviews.

Also setNeedsUpdateConstraints makes sure a future call to updateConstraintsIfNeeded calls updateConstraints.

so when layoutSubviews is called, it also calls updateConstraintsIfNeeded, so calling it manually is rarely needed in my experience