4

Consider a view with a custom background that is drawn in drawRect:. If the view changes size, the background need to be redrawn.

Is this a bad idea?

- (void) layoutSubviews
{
    [super layoutSubviews];
    [self setNeedsDisplay];
}

If it is, what would be a better alternative considering I can't control who changes the size of the view?

hpique
  • 119,096
  • 131
  • 338
  • 476

1 Answers1

11

Dont do that, it's not necessary. Set the contentMode of the view to UIViewContentModeRedraw:

UIViewContentModeRedraw

Redisplays the view when the bounds change by invoking the setNeedsDisplay method. Available in iOS 2.0 and later. Declared in UIView.h.

This will achieve the same effect.

Community
  • 1
  • 1
jrturton
  • 118,105
  • 32
  • 252
  • 268
  • +1 for teaching me something new. Also, it appears to work! :) – hpique Nov 17 '12 at 22:04
  • 2
    Note that's it's also fine to send `setNeedsDisplay` in `layoutSubviews`, which you might want to do if you only need to redraw on some size changes. – rob mayoff Nov 17 '12 at 22:53
  • Can you please tell me where exactly to add a line of code to set `contentMode` is it in `init:` , `layoutSubviews()` or ? Because i have added it in `init:` but it dint work for me. – Sunil Rao Apr 26 '16 at 10:43
  • @SunilRao wherever you create the view should be fine – jrturton Apr 26 '16 at 12:45