layoutIfNeeded vs setNeedsLayout
layoutIfNeededsays update immediately please, whereas
setNeedsLayoutsays update but you can wait until the next UI update cycle.
Using more words
The method 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 any changes that were noted prior to the method call. doc ↗
The method tells the system that you want it to layout and redraw that view, and all of its subviews, during the update views cycle. This is an asynchronous activity, because the method completes and returns immediately. It's only later (milliseconds) that the layout and redrawing actually happens. You just don’t know when exactly. doc ↗
Names can be deceiving
⚠️ Watch out, you may find the naming a little deceiving regarding what it does.
Remember, here, the if needed means do it immediately. Which is a bit confusing IMO.
This article paraphrases a great article from Lawrence MacFadyen ↗ that is no more available on the web, unfortunately.
The link in case it reappears one day: iosinsight.com ↗.
Hope it helps. Drop a ❤️ if you found it helpful, thanks 🙂