setNeedsDisplay doesn’t call draw(_ rect:)

Goal栈
1 min readAug 9, 2018

As the title mentioned I came across this situation:

If I want to redraw my view(subclass of UIView) I can simply call setNeedsDisplay method. Everything works perfect until I try to redraw the UIView after it’s presented view controller dismissed, it doesn’t work, which means the draw(_rect:) wasn’t called, why?

I think firstly we should know about setNeedsDiplay() :

When the actual content of your view changes, it is your responsibility to notify the system that your view needs to be redrawn. You do this by calling your view’s setNeedsDisplay()or setNeedsDisplay(_:) method of the view.

setNeedsDisplay

Marks the receiver’s entire bounds rectangle as needing to be redrawn.

setNeedsDisplay will only call drawRect: if the view is visible.

So the solution is very simple call it after viewDidAppear.

--

--