Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is layoutSubviews called whenever view's size is changed?

In my impression, with autoresizesSubviews = YES, layoutSubviews should be called every time view's size is changed. But I found it is not the case for my view. Is my expectation wrong?

like image 649
an0 Avatar asked Sep 04 '11 00:09

an0


1 Answers

If you need something to happen when your view is resized, you can also override setBounds: and setFrame: for your class to make sure it happens. It would look something like this

-(void)setBounds:(GCRect newBounds) {
    // let the UIKit do what it would normally do
    [super setBounds:newBounds];

    // set the flag to tell UIKit that you'd like your layoutSubviews called
    [self setNeedsLayout];
}

-(void)setFrame:(CGRect newFrame) {
    // let the UIKit do what it would normally do
    [super setFrame:newFrame];

    // set the flag to tell UIKit that you'd like your layoutSubviews called
    [self setNeedsLayout];
}


The other reason that I sometimes override these methods (temporarily) is so I can stop in the debugger and see when they are getting called and by what code.

like image 192
Mike Hay Avatar answered Nov 09 '22 00:11

Mike Hay