Search code examples
iosnslayoutconstraint

Constraint "equal heights" for views of different sizes in iOS?


I saw a lot of similar questions but they are all for situations with equal views.

In my case I have a vertical stack of views: enter image description here And they should keep proportions simultaneously on different devices.

Could you advice how to achieve such goal? Xib or programmatically - it makes no odds.

Currently I'm trying to set their sizes manually.


Solution

  • Define equal height constraint, than edit it and set multiplier to e.g. "1:5".

    Example

    Update:

    Choose a vertical stack view, add your 4 views, mark all of them, set the constraint "equal height" and add all 3 constraints:

    Step 1

    Now select each constraint and set your multiplier as you like in relation to the first:

    Step 2

    Step 3

    Step 4

    Define a spacing in stack view settings:

    Step 5

    Define StackView border gaps relative to margin or via const value:

    Step 6

    You do not need more constraints. Proof by change device and orientation:

    enter image description here