简体   繁体   中英

XCode 7 Autolayout Constraints: How to make 2 images stay horizontally in the center

I'm trying to position 2 UIImage views in the center of the screen, side by side. If it's just 1 UIImage, I can just set them to be vertically and horizontally center in the container. If I have 2 UIImage views side by side, I can only set it to be vertically centered, but not horizontally centered since both UIImage views would stack over each other..

Could someone please guide me ? Thanks and greatly appreciated!

Take a view, put both the images inside that view. Add the constraints on those images with respect to the view you made (Edge to edge to the view). Now add the constraints on the view in respect of the super view. (Center vertically and horizontally). That's it!

An elegant way to solve this is to apply constraints on UIImageView itself.

Now think that if you need to have both the image views in superview's centre then one imageView should have it's trailing edge at the centre of superview and another should have a leading edge at the centre.

Control-Click on imgvLeft and drag it to superview , you will see some constraint option to select; choose trailing edge constraint. Now go to that constraint and click it you will see details as shown in picture below. Change the second item from trailing to centreX and keep the constant 0.
Same way you can apply the leading constraint on imgvRight and change the second item to centreX .

Now if you wish to keep some distance between both the views then you can change the constant value in constraints. For example if you need an spacing of 20 px then change the constant to spacing/2 ie 10.

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM