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, ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access