When do I need to call setNeedsDisplay in iOS?
Asked Answered
P

5

70

When creating an iOS app, I'm confused as to when exactly I need to call setNeedsDisplay? I know that it has something to do with updating/redrawing the UI; however, do I need to call this every time I change any of my views?

For example, do I need to call it:

  • After programatically changing the text in a text field
  • When changing the background of a view?
  • When I make changes in viewDidLoad?
  • How about in viewDidAppear?

Could someone give me some general guidelines regarding when to use this method?

Phelips answered 30/5, 2012 at 14:28 Comment(1)
It's one of the worst-named calls in all of computing. It should simply be called setNeedsDraw. setNeedsLayout tags the layout (constraints, frames) to be redone. setNeedsDraw tags the draw#rect call to be redone. It's that simple.Partizan
A
130

You should only be calling setNeedsDisplay if you override drawRect in a subclass of UIView which is basically a custom view drawing something on the screen, like lines, images, or shapes like a rectangle.

So you should call setNeedsDisplay when you make changes to few variables on which this drawing depends and for view to represent that change , you need to call this method which internally will give a call to drawRect and redraw the components.

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

Example:

You have a view that shows a moving circle, either you touch and move it, or may be timer based animation. Now for this, you will need a custom view that draws a circle at given center and with given radius. These are kept as instance variables which are modified to move the circle by changing its center or make it bigger by increasing radius of it.

Now in this case either you will modify these variables(centre or radius) in a loop and timer Or may be by your fingers in touchesEnded and touchesMoved methods. To reflect the change in this property you need to redraw this view for which you will call setNeedsDisplay.

Apoplectic answered 30/5, 2012 at 14:32 Comment(1)
Same for after I change the frame of a layer or view inside my view subclass?Metalanguage
T
11

You only really need to call -setNeedsDisplay on UIView subclasses that draw their contents with -drawRect:.

For labels and other standard controls, changing the text will automatically cause the label to redraw so you don't need to do this yourself.

Tootsy answered 30/5, 2012 at 14:33 Comment(0)
F
6

setNeedsDisplay: should be called when you want to refresh your view explicitly. It just sets an internal flag, and the iOS UI system will call drawRect: at an appropriate time later.

It sounds like it should be always called when you updating any property which may change the presentation. But it's not. Almost all the standard UI controls already handled that. I believe whenever you modify the properties of standard UI components (views), setNeedsDisplay: would be triggered internally, and the affected region will be redrawn. (In all the situations you listed)

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

Freestanding answered 30/5, 2012 at 14:40 Comment(0)
B
3

I think @Amogh Talpallikar make it clear. And I just wanna discuss one thing more.

In the fact that, you should avoid override drawRectunless you really need it because it can cause bad performance. You can refer this https://yalantis.com/blog/mastering-uikit-performance/

If you only wanna change frame, position of buttons, labels, ... you can call setNeedLayout or layoutIfNeeded

Byblow answered 29/6, 2016 at 4:8 Comment(0)
S
1

You will call setNeedDisplay when you are changing the property on which your view custom drawing depends. It will explicitly call drawRect: method forcefully.

Sunlit answered 1/4, 2017 at 11:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.