How to align UIImageView with Aspect Fit to Top?

Emil Adz picture Emil Adz · Jan 21, 2016 · Viewed 7.1k times · Source

I'm working on some screen in which at top I have an application logo: I want this logo to be resized based on the free size it has. So I configured it with the desired constraints and when it has enough space it like the desired result:

enter image description here

The problem starts when the UIImageView does not have enough space to spread in this case the image is centred in the UIImageView and leaves an empty space above it:

enter image description here

Now I tried to place the UIImageView inside a simple UIView and place it accordingly inside, but so far without result.

I also tried to search for an answer here but all the solutions I have found:

how to set image in top avoiding space in UIimageView

Alignment UIImageView with Aspect Fit

and a custom view:

https://github.com/reydanro/UIImageViewAligned

were in Objective-C when I'm using Swift. How can I achieve this using the Storyboard on Swift?

Answer