Search code examples
iosautolayoutconstraintspercentageaspect-ratio

How to set height of the views in percentage of parent view size


I have 4 views and I want them to occupy 40 %,15%,15%,20% and I want 2 % space among them and 2% space to top and bottom layout.

I am confused on what constraint to use and how to use ?

like can aspect ratio be used for this like that ?

If I could set it in px I can do easily ,but its percetage there it gets tricky

Is it possible if i set it in px it will get translated to percentage according to screen size

Regards,

Appu


Solution

  • You can use the Equal constraint and set the percentages in the multiplier.

    Equal Constraint

    Assuming you have 1 view controller and 1 view in it, control + click (or right click) and drag from view to view controller, and choose equal (width | height).

    Contextual

    After that, go to the size inspector on the right side and click edit on the constraint you just added and change your multiplier.

    Edit

    Unrelated: Aspect ratio is when you want to constrain the size of a view to a certain ratio (W:H). 1:1 would mean width is equal to height. 1:3 means height is 3 times your width.

    Size classes is when you want to use different designs for different devices sizes. Read more on size classes here