What is the real difference between UIView
methods setNeedsLayout
and setNeedsDisplay
?
As usual documentation is foggy about this.
setNeedsLayout()Invalidates the current layout of the receiver and triggers a layout update during the next update cycle.
layoutSubviews() The system calls this method whenever it needs to recalculate the frames of views, so you should override it when you want to set frames and specify positioning and sizing. However, you should never call this explicitly when your view hierarchy requires a layout refresh.
If you want to force a layout update, call the setNeedsLayout() method instead to do so prior to the next drawing update. If you want to update the layout of your views immediately, call the layoutIfNeeded() method. For a better understanding , download and run the code in Github.
All you need is to call layoutIfNeeded and your views will update. To be more correct you don't even need to call layoutIfNeeded as it will do that for you in the next cycle. But you will need to call it if you want the change animated for instance and you need to do that in animation block.
Actually the documentation is pretty clear about this:
setNeedsLayout will layout subviews
Call this method on your application’s main thread when you want to adjust the layout of a view’s subviews.
setNeedsDisplay will call for a redraw of your view (drawRect:
, etc).
You can use this method or the setNeedsDisplayInRect: to notify the system that your view’s contents need to be redrawn.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With