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 ?
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.
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.
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.
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With