Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Center a UIView with auto layout is just not working

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 ?

like image 590
Curnelious Avatar asked Sep 16 '14 10:09

Curnelious


People also ask

How to set Auto Layout constraints in ios?

To create constraints select the button and click the Align icon in the auto layout menu. A popover menu will appear, check both “Horizontal in container” and “Vertically in container” options to center the button on the screen. Then click the “Add 2 Constraints” button. Run the application.

What are two properties that auto Layout constraints control on a UIView?

Auto Layout defines margins for each view. These margins describe the preferred spacing between the edge of the view and its subviews. You can access the view's margins using either the layoutMargins or layoutMarginsGuide property. The layoutMargins property lets you get and set the margins as a UIEdgeInsets structure.

How to set constraints in ios storyboard?

Select the view you would like to constrain. Then tap the button to the right of the one you have selected and use that menu to define your autolayout constraints. If you want it to work for all devices make sure your storyboard is on the wAny hAny size class setting.


1 Answers

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 storyboard according to constraints, just select the view itself and use update frames:

enter image description here

Edit:

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




EDIT 2:

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

enter image description here

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

EDIT 3:

To clarify what button to press:

enter image description here

like image 103
ullstrm Avatar answered Sep 17 '22 11:09

ullstrm