I want to use AutoLayout to size and layout a view in a manner that is reminiscent of UIImageView’s aspect-fit content mode.

I have a subview inside a container view in Interface Builder. The subview has some inherent aspect ratio which I wish to respect. The container view’s size is unknown until runtime.

If the container view’s aspect ratio is wider than the subview, then I want the subview’s height to equal the parent view’s height.

If the container view’s aspect ratio is taller than the subview, then I want the subview’s width to equal the parent view’s width.

In either case I wish the subview to be centered horizontally and vertically within the container view.

Is there a way to achieve this using AutoLayout constraints in Xcode 6 or in previous version? Ideally using Interface Builder, but if not perhaps it is possible to define such constraints programmatically.

8 Answers
8

Leave a Reply

Your email address will not be published. Required fields are marked *