Recipe: Creating Fixed-Size Constrained Views

When working with constraints, start thinking about your views in a new way. You don’t just set a frame and expect the view to stay where and how big you left it. Constraint layout uses an entirely new set of assumptions.

Here’s how you might have written a utility method to create a label before Auto Layout:

- (UILabel *)createLabelTheOldWay:(NSString *)title {     UILabel *label = [[UILabel alloc]        initWithFrame:CGRectMake(0.0f, 0.0f, 100.0f, 100.0f)];     label.textAlignment = NSTextAlignmentCenter;     label.text = title;     return label; }

With Auto Layout, you approach code-level view creation in a new way. Your code adds constraints that adjust the item’s ...

Get The Core iOS Developer’s Cookbook, Fifth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.