iOS swift — setNeedsLayout vs layoutIfNeeded vs layoutSubviews()

Abhimuralidharan
3 min readJul 4, 2017

I always wondered about these methods while coding in iOS. I tried to read more and this is what I understood.

When an iOS app launches, UIApplication in iOS starts the main run loop for an app, which runs on the main thread. The main run loop processes events (such as user touches) and handles updates to view-based interfaces. As events occur, such as touch, location updates, motion, and multimedia control, the run loop finds the appropriate handler for the events, calling appropriate methods, which call other methods, and so on. At some moment in time, all events will have been handled and control will return to the run loop. Let’s label this point where control is returned to the run loop as the update cycle.

While the events are being processed, and some changes are requested to the view, these changes are not updated immediately. Instead the system wait for the existing process to finish and when the next redraw cycle is going to happen. There is a periodic interval between the event processing and UI layout update handling. This is why we need to understand the above three methods properly.

— 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

--

--