72

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

As usual documentation is foggy about this.

mfaani
  • 33,269
  • 19
  • 164
  • 293
Duck
  • 34,902
  • 47
  • 248
  • 470

1 Answers1

104

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.

Joris Kluivers
  • 11,894
  • 2
  • 48
  • 47
  • 5
    no, I was wondering more in terms of both are used to redraw the view, right? Both will wait until the next draw cycle to be redrawn, right? – Duck Jan 24 '13 at 17:26
  • 27
    Layout is not about drawing. Layout is about positioning/sizing subviews. But yes, both delay to the next runloop cycle. So calling one multiple times in a row will only result in one relayout/redraw. – Joris Kluivers Jan 24 '13 at 17:27
  • 5
    Please provide an example of when `setNeedsDisplay` is needed. – Pwner Dec 03 '13 at 23:46
  • 2
    Call `setNeedsDisplay` to make sure your view redraws itself. A common use case is to call this after changing a property of your class that's used during drawing. – Joris Kluivers Dec 05 '13 at 12:20
  • 11
    @Pwner Say you want to draw a line between two dots (two UIView, exactly), this is implemented in `drawRect:` of **DrawLineView**. After you change the position of one button or both of them, you need to call `[drawLineView setNeedsDisplay];` to redraw the line between new position of the two dots. – fujianjin6471 May 31 '15 at 12:45
  • 5
    @Pwner http://blog.fujianjin6471.com/2015/06/11/An-example-of-when-should-setNeedsDisplay-be-called.html – fujianjin6471 Jul 06 '15 at 11:30