Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setNeedsLayout and setNeedsDisplay

What is the real difference between UIView methods setNeedsLayout and setNeedsDisplay?

As usual documentation is foggy about this.

like image 505
Duck Avatar asked Jan 24 '13 17:01

Duck


People also ask

What is setNeedsLayout?

setNeedsLayout()Invalidates the current layout of the receiver and triggers a layout update during the next update cycle.

When should I use layoutSubviews?

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.

When should I call layoutIfNeeded?

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.

How do I contact setNeedsLayout?

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.


1 Answers

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.

like image 192
Joris Kluivers Avatar answered Oct 11 '22 22:10

Joris Kluivers