When do I need to call setNeedsDisplay in iOS?

When creating an iPhone / iPad app, am I confused when do I need to call setNeedsDisplay? I know this has something to do with updating / redrawing the user interface; however, do I need to evoke this every time I change any of my views?

For example, I need to call it:

  • After programmatically changing text in a text box
  • When changing the background of the view?
  • When do I make changes to viewDidLoad?
  • What about viewDidAppear?

Can someone give me some general recommendations regarding using this method? Thanks

+59
ios objective-c iphone xcode ipad
May 30 '12 at 14:28
source share
5 answers

You should only call setNeedsDisplay if you override drawRect in a subclass of UIView, which is basically a custom view that draws something on the screen, such as lines, images, or shapes, such as a rectangle.

So, you must call setNeedsDisplay when you make changes to several variables that this drawing depends on, and to represent this change, you need to call this method, which internally will call drawRect and redraw the components.

When you add imageView or UIButton as a subtitle or make changes to any subtitle, you do not need to call this method.

Example:

You have a view showing a moving circle, either touching, or moving, or timer-based animations. Now for this you need a custom view that draws a circle in this center and with a given radius. They are saved as instance variables that change to move the circle, changing its center or increasing its radius.

Now in this case, either you will change these variables (in the center or in the radius) in the loop and timer. Or maybe your fingers are tapped and touched the simulated methods. To reflect the change in this property, you need to redraw this view for which you call setNeedsDisplay.

+121
May 30 '12 at
source share

You really need to call -setNeedsDisplay in subclasses of UIView that draw their contents with -drawRect:

For shortcuts and other standard controls, changing the text will automatically redraw the shortcut, so you don’t have to do it yourself.

+9
May 30 '12 at 14:33
source share

setNeedsDisplay: should be called if you want to explicitly update your view. It simply sets the internal flag, and the iOS user interface system calls drawRect: at the appropriate time later.

It seems like it should always be called when updating any property that might change the presentation. But this is not so. Almost all standard user interface controls have already handled this. I believe that whenever you change the properties of the standard components of the user interface (view), setNeedsDisplay: will be launched from the inside, and the affected region will be redrawn. (In all these situations)

However, if you create your own view, implement its own drawRect: and want to update it when something has changed, you must explicitly call setNeedsDisplay:

+5
May 30 '12 at 14:40
source share

I think @Amogh Talpallikar to clarify the situation. And I just want to discuss one more thing.

That you should avoid overriding drawRect if you really don't need it, because it can lead to poor performance. You can refer to this https://yalantis.com/blog/mastering-uikit-performance/

If you want to change the frame, the position of the buttons, shortcuts ... you can call setNeedLayout or layoutIfNeeded

+1
Jun 29 '16 at 4:08
source share

You call setNeedDisplay when you change the property your custom drawing depends on. It will explicitly call the drawRect: force method.

+1
Apr 01 '17 at 11:26 on
source share



All Articles