Is layoutSubviews called when resizing a view?

In my experience, with autoresizesSubviews = YES, layoutSubviews should be called every time the view is resized. But I found that this does not apply to my opinion. Is my expectation wrong?

+8
ios cocoa-touch uiview
source share
4 answers

According to sources at Apple

"- [UIView layoutSubviews] should be called when the view is resized.


They also referred to this from the iOS programming guide:

"Whenever the view size changes, UIKit applies the autoresistance behavior of these views and then calls the layoutSubviews method of the view to make changes manually. You can implement the layoutSubviews method in custom views when the autoresistance behavior alone does not produce the desired results."


At this stage, your best step is to create a small sample project in which layoutSubviews will not call (or send your existing project) a file from Apple using BugReporter , and include this sample project with your error.

+6
source share

If you need something to happen when your view changes, you can also override setBounds: and setFrame: for your class to make sure it happens. It will 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]; } 


Another reason I sometimes override these methods (temporarily) is because I can stop in the debugger and see when they get called and with which code.

+1
source share

From my understanding, layoutSubviews is called when the bounds view changes. This means that if its position changes in its supervisor (but not its size), then layoutSubviews will not be changed (since the starting point within the boundaries is in the coordinate system of the view, therefore it is almost always equal to 0.0). In short, only resizing will result in dismissal.

0
source share

when you want to manually resize views and automatically resize, the layoutSubViews method is called

 -(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; } 
-one
source share

All Articles