Take the 2-minute tour ×

I am trying to better understand how to control the size of Views in terms of older iphones and the new iphones difference in size.

If I have a design like the below example, how should this be coded programatically in terms of the subviews.


Excuse the badly drawn diagram but it should help to explain.

In this example, the fieldView and buttonView would always need to remain a fixed size as they have objects which would not look great when made smaller. However the logoview has another sub view for the logo itself, so could be shrinked depending on device/screen size.

How would this be accomplished? Setting up the example subviews programatically. The part I do not understand is that in viewDidLoad where the subviews are created would you not have to create in order like this:

-(void)ViewDidLoad {
CGRect screen = [[UIScreen mainScreen] applicationFrame];
wholeView = [[UIView alloc] initWithFrame:CGRectMake(self.view.bounds.origin.x, self.view.bounds.origin.y, self.view.bounds.size.width, self.view.bounds.size.height)];

 logoView = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,150);
fieldView = [[UITableView alloc] initWithFrame:CGRectMake(0, logoView.bounds.size.height, 320, 100);

I understand about using


but how would it come into use in terms of working out a height depending on the actual view size available?

share|improve this question

1 Answer 1

up vote 1 down vote accepted

Not sure I understand your doubts, but adding the following should do the trick:

self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
logoView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

The remaining view would keep both their margins and height/width fixed (i.e., the default value of UIViewAutoresizingNone for their autoresizingMask is fine.)


basically, what you see on your iPhone display is a hierarchy of views; the topmost view in this hierarchy is a UIWindow. This has the same size as the device screen (it is initialized that way).

Now, in the code above, our logoView has fixed margins: this means that it will remain at the same distance from the container view frame; if we specify that is size is flexible, then the logoView will simply occupy the whole space to keep the margins fixed.

share|improve this answer
Ok so basically I setup the logoView with 0,0,fixed height, fixed width)... then setup the other views with 0,(view above's height),fixed height, fixed width... Same applies for buttonView. Then set only the loginView and self.view to have an autoresizingMask for Width and Height. In turn this will stretch only that logoView when required to fit to screen? –  StuartM Feb 21 '13 at 14:58
right: you set all the views' frames so that they appear correctly on screen at a fixed size, then specify flexibleWidth/Height for logoView so that it will extend to take all the available space. you also need to specify flexible height for the container view, because if it does not stretch as well there will be no available space for the logoView to occupy. –  sergio Feb 21 '13 at 15:02
So although you actually set a fixed height and width for logoView it is in fact ignored by using the autoresizingMask? –  StuartM Feb 21 '13 at 15:10
if by "set a fixed height and width for logoView" you mean what you specified in initWithFrame, well, you can view that as a "reference" width/height, which affects the way autoresizing works. what could help understanding this is thinking of a Mac: you have a window on screen that you can make larger or smaller; if you resize the window, then the window's subviews dynamically rearrange themselves based on their current state. on the iPhone, since your main window has a fixed size, autoresizing only comes into play with different devices and with device rotation. –  sergio Feb 21 '13 at 15:15
Awesome, top job –  StuartM Feb 21 '13 at 15:56

Your Answer


