Chapter 16 Auto layout: Programmatic Constraints
Chapter 16 Auto layout: Programmatic Constraints
1. The line of code regarding translating constraints has to do with an older system for scaling interfaces - autoresizing masks. Before Auto Layout was introduced, iOS applications used autoresizing masks to allow views to scale for different-sized screens at runtime.
-(void)viewDidLoad
{
[super viewDidLoad];
UIImageView *iv = [[UIImageView alloc] initWithImage:nil];
// The contentMode of the image view in the XIB was Aspect Fit;
iv.contentMode = UIViewContentModeScaleAspectFit;
// Do not produce a translated constraint for this view
iv.translatesAutoresizingMaskIntoConstraints = NO;
// The image view is a subview of the view
[self.view addSubview:iv];
// The image view was pointed to by the imageView property
self.imageView = iv;
}
2. Every view has an autoresizing mask. By default, iOS creates constraints that match the autoresizing mask and adds them to the view. These translated constraints will often conflict with explicit constraints in the layout and cause an unsatisfiable constraints problem. The fix is to turn off this default translation by setting the property translatesAutoresizingMaskIntoConstraints to NO.
3. Apple recommends using a special syntax called Visual Format Language (VFL) to create constraints programmatically. VFL is a way of describing constraints in a literal string. You can describe multiple constraints in one visual format string. A single visual format string, however, cannot describe both vertical and horizontal constraints.
Here is the horizontal spacing constraints for the image view as a visual format string:
@“H : | -0- [imageView] -0-|”
The H: specifies that these constraints refer to horizontal spacing. The view is identified inside square brackets. The pipe character (|) stands for the view’s container. This image view, then, will be 0 points away from its container on its left and right edges.
When the number of points between the view and its container (or some other view) is 0, the dashes and the 0 can be left out of the string:
@“H: | [imageView] | “
The string for the vertical constraints looks like this:
@“V : [dateLabel] -8- [imageView] -8- [toolbar]”
Notice that “top” and “bottom” are mapped to “left” and “right”, respectively, in this necessarily horizontal display of vertical spacing. The image view is 8 points from the date label at its top edge and 8 points from the toolbar at its bottom edge.
The dash by itself sets the spacing to the standard number of points between views, which is 8. You could write this same string like this:
@“V : [dateLabel] - [imageView] - [toolbar]”
4. A constraint is an instance of the class NSLayoutConstraint. When creating constraints programmatically, you explicitly create one or more instances of NSLayoutConstraint and then add them to the appropriate view object. Creating and adding constraints is one step when working with a XIB, but it is always two distinct steps in code. You create constraints from a visual format string using the NSLayoutConstraint method:
+(NSArray *)constraintsWithVisualFormat:(NSString*)format options:(NSLayoutFormatOptions)opts metrics:(NSDictionary*)metrics views:(NSDictionary*)views
This method returns an array of NSLayoutConstraint objects because a visual format string typically creates more than one constraint.
5. Add instances of NSLayoutConstraint to the view using the UIView method
-(void)addConstraints:(NSArray*)constraints
Usually, the closest common ancestor of the views that are affected by the constraint. Here is a list of rules you can follow to determine which view you should add constraints to:
If a constraint affects two views that have the same superview (such as the constraint labeled “A” in Figure 16.1), then the constraint should be added to their superview.
If a constraint affects just one view (the constraint labeled “B”), then the constraint should be added to the view being affected.
If a constraint affects two views that do not have the same superview but do share a common ancestor much higher up on the view hierarchy (the constraint labeled “C”), then the first common ancestor gets the constraint.
If a constraint affects a view and its superview (the constraint labeled “D”), then this constraint will be added to the superview.
6. Intrinsic content size is information that a view has about how big it should be based on what it displays. For example, a label’s intrinsic content size is the size of the image that you selected.
Auto layout takes this information into consideration by creating intrinsic content size constraints for each view. Unlike other constraints, these constraints have two priorities: a content hugging priority and a content compression resistance priority.
Content hugging priority: tells Auto Layout how important it is that the view’s size stay close to, or “hug”, its intrinsic content. A value of 1000 means that the view should never be allowed to grow larger than its intrinsic content size. If the value is less than 1000, then Auto Layout may increase the view’s size when necessary.
Content compression resistance priority: tells Auto Layout how important it is that the view avoid shrinking, or “resist compressing”, its intrinsic content. A value of 1000 means that the view should never be allowed to be smaller than the view should never be allowed to be smaller than its intrinsic content size. If the value is less than 1000, then Auto Layout may shrink the view when necessary.