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

后端 未结 2 468
轻奢々
轻奢々 2021-01-28 07:03

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 c

相关标签:
2条回答
  • 2021-01-28 08:09

    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!

    0 讨论(0)
  • 2021-01-28 08:09

    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 i.e. 10.

    0 讨论(0)
提交回复
热议问题