Laying out & sizing of subviews in a UIViewController
You can do your layout logic inside the viewWillLayoutSubviews
of the UIViewController
.
-(void)viewWillLayoutSubviews{
[super viewWillLayoutSubviews];
// Your layout logic here
}
DOC: Called just before the view controller's view's layoutSubviews method is invoked. Subclasses can implement as necessary. The default is a nop.
autoresizesSubviews
should be set on your parent view, while autoresizingMask
should be set on the child views - this is the mistake I made so you could, too.
In loadView
you should size your subviews to fit whatever size of parent view is at the moment, and then later on when parent view is resized from 460 to 367 pixels your sub-views will be resized as well, according to your mask settings above.
If that fails, there is nothing wrong in setting the view size within viewWillAppear
- the performance impact of doing it every time is negligible.
If nothing else works, there is always layoutSubviews:
- there you could do manual layout if you have to, it's invoked when system believes layout may have to change. there is also setNeedsLayout
: that I sometimes invoke from viewWillRotate:
/viewDidRotate
: etc. But really this shouldn't be needed and autoresize should be good enough.
EDIT: Yes, to implement custom layout logic in layoutSubviews
as I mention above one would need to subclass UIView
.