iOS Dev Nugget 35 Use UIView.autoresizingMask


Need to run a code review on your codebase? Hire me

Often, you might only support portrait orientation in an app, especially for a v1. However, you'd still want to think about about how your views and controls resize, especially both vertically and if there are right margins.

Autoresizing for a view controller's view (or a subview that should have the same size as it's parent view) is important because it not only helps you when you want to target a taller device (e.g. when iPhone 5 was just released) as well as when you want to handle changes in height due to status bar, in-call indicator bar, change of device orientation and showing and hiding of different keyboards.

Generally, you'd want to do this for views that occupy the screen:

UIView* bigView = [[UIView alloc] initWithFrame:[UIScreen mainScreen]]; bigView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

For a control that is right aligned (with a right margin of 5pts), you probably want something like:

UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(0, 100, 100, 20)]; label.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin; label.moRight = self.moWidth - 5;

-moRight and moWidth are category extensions I added to UIView for convenience. You can find them at here on GitHub.

Your feedback is valuable: Do you want more nuggets like this?   Yes   or   No


Like this and want such iOS dev nuggets to be emailed to you, weekly?

Sign Me Up! or follow @iosdevnuggets on Twitter


View archives of past issues