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 based on how much text it is displaying. In your case, the image view’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 |
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 |
In addition, both priorities have separate horizontal and vertical values so that you can set different priorities for a view’s height and width. This makes a total of four intrinsic content size priority values per view.
You can see and edit these values in Interface Builder. Reopen BNRDetailViewController.xib. Shift-click to select all three text fields in the canvas. Head to the inspector and select the tab to reveal the size inspector. Find the Content Hugging Priority and Content Compression Resistance Priority sections.
First, notice that these values are not 1000
and thus will never conflict with the constraints that you have added so far.
This is why the layout will appear incorrectly with smaller-sized images.
The value text field’s content hugging vertical property is 250, which is lower than that of the image view (which is 251), so when faced with a small image, Auto Layout chooses to make the text field taller than its intrinsic content size.
It would be better if the image view had a smaller vertical content hugging and commpression resistance priority than the other subviews. Open BNRDetailViewController.m and update viewDidLoad to lower these priorities.
- (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 was a subview of the view [self.view addSubview:iv]; // The image view was pointed to by the imageView property self.imageView = iv; // Set the vertical priorities to be less than // those of the other subviews [self.imageView setContentHuggingPriority:200 forAxis:UILayoutConstraintAxisVertical]; [self.imageView setContentCompressionResistancePriority:700 forAxis:UILayoutConstraintAxisVertical]; ... }
Build and run again. Now, when dealing with smaller-sized images, Auto Layout will change the image size and leave the height of the text fields alone.