Putting It Together

Using these tools, we can replace the old label-creation method above with a much more powerful variant that uses Auto Layout:

- (UILabel *)createLabelWithTitle:(NSString *)title     onParent:(UIView *)parentView {     UILabel *label = [[UILabel alloc] init];     label.textAlignment = NSTextAlignmentCenter;     label.text = title;     // Add label to parent view so constraints can be added     [parentView addSubview:label];     // Turn off automatic translation of autoresizing masks into constraints     label.translatesAutoresizingMaskIntoConstraints = NO;     // Add constraints     [label constrainWithinSuperviewBounds];     [label constrainSize:CGSizeMake(100, 100)];     [label constrainPosition:CGPointMake(50, ...

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.