an0
an0

Reputation: 17530

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?

Upvotes: 10

Views: 11658

Answers (4)

Mahesh Aswathanarayana
Mahesh Aswathanarayana

Reputation: 161

whenever you want to resize the views manually and resizes automatically call layoutSubViews method

-(void)layoutSubviews
{

    [super layoutSubviews];

    CGRect contentRect = self.contentView.bounds;

    CGFloat boundsX = contentRect.origin.x;

    CGRect frame,itemlabelframe,statuslabelframe;

    frame= CGRectMake(boundsX+1 ,0, 97, 50);

    itemlabelframe=CGRectMake(boundsX+100, 0, 155, 50);

    statuslabelframe=CGRectMake(boundsX+257, 0, 50, 50);
    ItemDescButton.frame=itemlabelframe;


    priorityButton.frame = frame;
    statusButton.frame=statuslabelframe;
    //    ItemDescLabel.frame=itemlabelframe;
    //    statusLabel.frame=statuslabelframe;

}

Upvotes: -1

Mike Hay
Mike Hay

Reputation: 2856

According to sources at Apple,

"-[UIView layoutSubviews] should get called when the size of the view changes."


They also referred me to this, from the the View Programming Guide for iOS:

"Whenever the size of a view changes, UIKit applies the autoresizing behaviors of that view’s subviews and then calls the layoutSubviews method of the view to let it make manual changes. You can implement the layoutSubviews method in custom views when the autoresizing behaviors by themselves do not yield the results you want."


At this point, your best move is to create a small sample project where layoutSubviews does not get called (or, send your existing project) file a bug with Apple using BugReporter, and include that sample project with your bug.

Upvotes: 7

Mike Hay
Mike Hay

Reputation: 2856

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.

Upvotes: 1

dtuckernet
dtuckernet

Reputation: 7895

From my understanding, layoutSubviews is called when the view's bounds change. This means that if its position changes in its superview (but not its size) then layoutSubviews won't be changed (since the origin point in the bounds is in the view's coordinate system - so it is almost always 0,0). In short, only a change in size will cause this to be fired.

Upvotes: 0

Related Questions