Search code examples
iosswiftautolayout

Swift: Image auto constraints with view background


I am starting out with Swift and tried to search for an answer but couldn't come across one. I have a UIViewController and within it, I am working the layout of components (labels, buttons, images, etc.).

For the sake of getting a nice look, I used UIView and changed its color to grey shade. Inside it I have an image of the logo and I am trying to apply auto constraints within it but I can't seem to get it right as for some reason it stretches the image out of the view.

I've added images for before the auto constrain and after auto constraint for your reference.

Before auto constraints:

Before constraints

After auto constraints:

After constraints

There are no constraints set in the code. Only in the Interface Builder.


Solution

  • Give Top, leading, trailing, aspect ratio constraints and set image content mode to Aspect fit. Attaching screenshot for your referenceenter image description here