Center a UIView with auto layout is just not working

ぃ、小莉子 提交于 2019-12-21 12:34:44

问题


I have UIImageView which is in the screen size (auto layouts are 0 to all neighbours) . Inside it, i have another UIImageView. , i would like it to always be in the Center of its parent imageview(hence on the Center of the screen) , and so it will change its size in relation to screen size, and keep aspect ratio .

So inside the storyboard(Xcode 6), i am trying to control drag from the Center view to its parent, than i add aspect ratio constrain ,than i see its aspect ratio,and that the constrain added.

Running this now, will NOT change image size and it stay in the same size for all screens.

Than in the same way , i want to Center it , so i control drag to its superview , than chose Center X and Center Y , needless to say that its not centred to any screen size.

Why is it has to be so hard to place a view in the Center ?


回答1:


It's super easy. See my images:

Then you need width/height-constraints:

And you're done!

To replace your view in storyboard according to constraints, just select the view itself and use update frames:

Edit:

If you want aspect-ratio width/height, just do this (The multiplier part):




EDIT 2:

Clarification of how to open the first window. Then just press the checkboxes to add the constraints:

These "center in container"-constraints should be the same as the added ones in this window (CTRL-drag to superview to open this):

EDIT 3:

To clarify what button to press:



来源:https://stackoverflow.com/questions/25866408/center-a-uiview-with-auto-layout-is-just-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!