cachvico cachvico - 2 months ago 23
Objective-C Question

How to redraw custom subviews of custom UIView

Let

MyView<UIView> *A
have an overridden
drawRect:


Let
A
contain a subview
MySubview<UIView> *B
also with an overridden
drawRect:


How do I get
B
to redraw when
[A setNeedsDisplay]
is called; is it the responsibility of
A
’s
drawRect:
implementation to call
[B drawRect:rect]
?

Thank you for any pointers.

Answer

-setNeedsDisplay: applies only to the view to which it is sent. If you need to redraw other views, you need to add some code to send -setNeedsDisplay: to them, too. That's all there is to it.

This is effectively for performance, you wouldn't always necessarily want all subViews to redraw.

If you do want to redraw all subViews, you can use this piece of code:

[self.subviews makeObjectsPerformSelector:@selector(setNeedsDisplay)];