How to Resize View so That Its Subviews Get Resized Too

user444854 picture user444854 · Sep 11, 2010 · Viewed 16.6k times · Source

My window adds a subview (my root ViewController's view).

Such subview is superview of several other subviews.

I have just finished an app, and now I want to add an ad.

Here's some code:

[window addSubview:viewController.view];
viewController.view.frame=CGRectMake(0,0,320,480-48);
viewController.clipToBounds=YES;
Ad *ad=[Ad alloc] initWithFrame:CGRectMake(0,480-48,320,48)];

The above viewController has several subviews, but they won't resize. The above viewController.view was 320,480 originally and was completely filled with subviews, until the last pixel on the bottom. And after I change its height from 480 to 460, the subviews do not resize, so on the bottom of the view (where the ad goes) some subviews aren't visible.

How can I get the subviews to resize so to fit the parent view (viewController.view), when the latter has its height reduced by 20px? (I am aware that they'll be deformed a bit)

Answer

Rob Napier picture Rob Napier · Sep 11, 2010

You need to set the autoresizing mask for the subviews:

ad.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

See the UIView documentation for more details.