iPhone SDK: How to trigger drawRect on UIView subclass after orientation change?
Asked Answered
C

3

21

I am subclassing a UIView and overwrite the drawRect method. I'm noticing that the view's drawrect is only being called when the view first loads. After that it is never called again. How to I make sure it gets called after an orientation change? I've tried calling setNeedsDisplay on the view before and after the rotation and that doesn't do it.

Cabinet answered 26/3, 2010 at 19:32 Comment(0)
A
60
[myView setContentMode:UIViewContentModeRedraw];

You can set this in IB as well (i.e., set mode to "redraw")

Ashlieashlin answered 26/1, 2012 at 18:32 Comment(2)
See the original author's response below. setNeedsDisplay is indeed the right method call.Vigilance
setNeedsDisplay: is used when you want to programmatically trigger a redraw. When you want orientation changes to trigger a redraw then setContentMode: is the ticket.Ashlieashlin
C
9

This was a bug related to something completely different. setNeedsDisplay does indeed cause drawRect to be called.

Cabinet answered 12/5, 2010 at 19:42 Comment(1)
Yes setNeedsDisplay: will trigger a redraw, but requires the programmer to explicitly invoke it. 'setContentMode:` will automagically trigger a redraw on an orientation change.Ashlieashlin
Q
0

to answer this and the other 94,000 similiar questions about view rotation/drawrect funkiness,

-(id)initWithFrame:(CGRect)frame
{
    if(self=[super initWithFrame:frame]) {
        self.autoresizesSubviews=YES;
        self.autoresizingMask=UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
    }
    return self;
}
Quito answered 7/2, 2013 at 15:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.