UIView center with auto layout just doesn't work

I have UIImageView

one that is in screen size ( auto layouts

equal to 0 for all neighbors). Inside it I have another one UIImageView.

, I would like it to always be in the Center of its parent imageview

(hence the Center of the Screen), and so it will resize itself in relation to the size of the screen, and keep the aspect ratio.

So, internally storyboard

(Xcode 6), I am trying to control drag from center view to its parent than add aspect ratio constrain

, than I can see its aspect ratio, and that constraint is added.

Running this now will NOT resize the image and will stay on the same screen for all screens.

Likewise, I want to Center it, so I control the drag in its supervisor than I chose Center X

and Center Y

it goes without saying that its not centered on any screen size.

Why is it so difficult to place in the Center?


source to share

1 answer

It's super easy. See my images:

Then you need width / height constraints:

enter image description here

And you're done!

To replace your view in the storyboard as per constraints, just select the view and use refresh frames:

enter image description here


If you want the width / height of the aspect ratio just do this (part of the multiplier): enter image description here


Explanation of how to open the first window. Then just check the boxes to add constraints:

enter image description here

These "center in container" contracts should be the same as added in this window (CTRL-drag to view to open this): enter image description here


To clarify which button to press:

enter image description here



All Articles