Search code examples
iosautolayoutaspect-ratioios-autolayout

How to make distance between two views variable in autolayout


I have a view which is 50 px down from the top layout.

I want the distance to vary based on the device size for example it should become 30 px in 4s while it should become 50px in iphone se while 60 in iPhone 6s.

I tried giving aspect ratio instead of vertical spacing but it not working.

If I just give vertical spacing its 50px in all devices.. Kindly help me out


Solution

  • @Aziz Javed propsed the correct solution depending on what you really want. If you want a specific constant values for each device the only way is to do it by code as Aziz Suggested.

    The storyboard constrant does not have an aspect ratio property for (Top,Bottom,Left,Right,Leading,Trailing) constraints.

    The work around solution in storyboard is to add a view on top of your view. Add a spacing constraints equal to zero to top and bottom to your view. On the added view that will play the spacing role add a height constraint with the aspect ratio set to screen view.

    enter image description here