I want my UIImageView
to grow or shrink depending on the size of what the actual image it's displaying is. But I want it to stay vertically centered and 10pts from the leading edge of the superview.
However, if I set these two constraints it complains I haven't set up enough constraints to satisfy Auto Layout. To me it seems to perfectly describe what I want. What am I missing?
If you read the docs on UIImageView they say:
Here is an
AspectFit
UIImageView
-derived implementation that works with Auto Layout when only one dimension is constrained. The other one will be set automatically. It will keep image aspect ratio and doesn't add any margins around the image.It's tweaked Objective-C implementation of @algal idea with the following differences:
priority = (UILayoutPriorityDefaultLow + UILayoutPriorityFittingSizeLevel) / 2.0
which evaluates to150
isn't enough to beat the priority of1000
of the default image content size constraint. So aspect constraint priority was increased to1000
as well.image
setter could be called multiple times, so it's a good idea to not cause extra layout calculations here.required public init?(coder aDecoder: NSCoder)
andpublic override init(frame:CGRect)
, so these two are taken out. They aren't overridden byUIImageView
, that's why there is no point of adding the aspect constraint until an image is set.AspectKeepUIImageView.h
:AspectKeepUIImageView.m
:Here is the essential problem: the only way in which
UIImageView
interacts with Auto Layout is via itsintrinsicContentSize
property. That property provides the intrinsic size of the image itself, and nothing more. This creates three situations, with different solutions.case 1: view with image's size
In the first case, if you place an
UIImageView
in a context where external Auto Layout constraints affect only its position, then the view'sintrinsicContentSize
will declare it wants to be the size of its image, and Auto Layout will automatically resize the view to equal the size of its image. This is sometimes exactly what you want, and then life is good!case 2: fully constrained view size, preserving image's aspect ratio
At other times, you're in a different case. You know exactly the size you want the image view to take, but you also want it to preserve the aspect ratio of the displayed image. In this case, you can use Auto Layout to constrain the size of the image, while setting the old
contentMode
property to.scaleAspectFit
on the image view. That property will cause the image view to rescale the displayed image, adding padding as needed. And if this is what you want, life is good!case 3: partially constrained view size, adapting to image aspect ratio
But often times, you're in the tricky third case. You want to use Auto Layout to partially determine the size of the view, while allowing the image's aspect ratio to determine the other part. For instance, you might want to use Auto Layout constraints to determine one dimension of the size (like the width, in a vertical timeline scroll), while relying on the view to tell Auto Layout that it wants a height matching the image's aspect ratio (so the scrollable items are not too short or too tall).
You cannot configure an ordinary image view to do this because because an image view only communicates its
intrinsicContentSize
. So if you put an image view in a context where Auto Layout constrains one dimension (for instance, constraining it to a short width), then the image view does not tell Auto Layout it wants its height rescaled in tandem. It continues to report its intrinsic content size, with the unmodified height of the image itself.In order to configure the image view to tell Auto Layout that it wants to take on the aspect ratio of the image it contains, you need to add a new constraint to that effect. Furthermore, you will need to update that constraint whenever the image itself is updated. You can build a
UIImageView
subclass that does this, so the behavior is automatic. Here's an example:More comment is available at this gist
For @algal case 3, all I had to do was
This scales the height component of the intrinsicContentSize. (-1.0, -1.0) is returned when there is no image because this is the default behavior in the superclass.
Also, I did not need to set
UITableViewAutomaticDimension
for the table with the cell containing the image view, and the cell still sizes automatically. The image view's content mode isAspect Fit
.The image view's intrinsic size is already dependent on the size of the image. Your assumptions (and constraints are correct).
However, if you've set up your image view in interface builder and have not provided it with an image, then the layout system (interface builder) won't know how big your image view is supposed to be at compile time. Your layout is ambiguous because your image view could be many sizes. This is what throws the errors.
Once you set your image view's image property, then the image view's intrinsic size is defined by the size of the image. If you're setting the view at runtime, then you can do exactly what Anna mentioned and provide interface builder with a "placeholder" intrinsic size in the property inspector of the image view. This tells interface builder, "use this size for now, I'll give you a real size later". The placeholder constraints are ignored at runtime.
Your other option is to assign the image to the image view in interface builder directly (but I assume your images are dynamic, so this won't work for you).