I am having trouble constraining images of various sizes to a fixed height in a Storyboard-based View Controller. Although the images have a height constraint and the vertical dimensions of the elements are all constrained from the top layout guide to the bottom layout guid, the images are displaying at their innate height and ignoring the height constraint.
The imageView in which the images are displayed has a height constraint in Storyboard of 128. The constraint is connected to a file using an IBOutlet and I further set the height constraint of the image in ViewDidLoad to 128.
.h file
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *messageImageHeight;
.m file
//in viewdidload
self.messageImage.image = [UIImage imageNamed:@"placeholder.png"];
_messageImageHeight.constant=128;
self.messageImage.clipsToBounds = YES;
self.messageImage.contentMode = UIViewContentModeScaleAspectFit;
I have also tried:
[self.messageImage layoutIfNeeded];
However, the images are displaying at their original heights. For example a 360×360 image is displaying with a height of 360 while a 512×512 image is displaying at a height of 512.
The images do respect their horizontal width. But the height goes to the original height of the image whether with ContentModes AspectFit, Aspect Fill or ScaleToFill
, with the only difference being that if I set a background color for the imageView, with AspectFit you see the color at the top and bottom. Storyboard does not show any errors with autolayout.
Can anyone suggest a fix for this or recommend how to troubleshoot it?
Thanks for any suggestions.