setNeedsLayout() vs layoutIfNeeded()

setNeedsLayout() of UIView tells the system that you want the current view and all its subviews to be redrawn in the next update cycle.

Whereas layoutIfNeeded() is a synchronous call which tells the system that you want the current view and all its subviews to be redrawn immediately without waiting for the next update cycle.

UIView have one more method named layoutSubviews() which you can use to calculate and set the frame of your view directly, in case auto-layout is not working for you as expected.

Thanks for reading.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.