Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using auto-layout to make a perfect square

I have a view with an imageview in it. I'd like to be able to make it so that the imageview always remains a perfect square, but I am not entirely sure how to implement this with autolayout.

My screen looks like so currently:

enter image description here

The imageview is what I'd like to be square, no matter the width dimension. I understand that this may be able to be done with constraints, and I have tried, but ultimately have been unable to figure out how to achieve this. This is perhaps due to my relative inexperience with autolayout, storyboards and ios development in general

like image 251
zillaofthegods Avatar asked Oct 24 '25 19:10

zillaofthegods


2 Answers

You should set 4 constraints for your square view: Top Spacing, Leading Spaces, Trailing Spaces and Aspect Ratio. This is shown bellow:enter image description here

Make sure that the aspect ratio of your square is 1:1 enter image description here

Hope this helps and good luck with Auto Layout!

like image 145
Nikita Arkhipov Avatar answered Oct 26 '25 09:10

Nikita Arkhipov


There is an aspect ratio constraint. You can set that to 1:1

like image 33
Krys Jurgowski Avatar answered Oct 26 '25 08:10

Krys Jurgowski



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!