Search code examples
iosuiviewautolayout

How to make grow UIViews proportionally


I have view simmilar to this below:

Views stack

How can I make it possible to make height of first and last view bigger proportionally, when making yellow view height smaller? Can I do it by setting content hugging priority and content compression?


Solution

  • Now that I apparently understand your problem, I suggest the following:

    • The top of the violet view has to be set equal to the top of the superview.
    • The height of the violet view is still undefined.
    • The bottom of you violet view has to be set equal to the top of the label.
    • The label has a fixed height.
    • The bottom of the label has to be equal to the top of the yellow view.
    • The yellow view has initially a fixed height.
    • The bottom of the yellow view has to be set equal to the top of the button.
    • The height of the button is still undefined.
    • The bottom of the button has to be set equal to the bottom of the superview.

    This leaves the heights of the violet view and the button undefined. But your requirement is that their heights are proportional.
    This can be achieved be setting them first to be equal to each other. Then double click the constrains and change the multipliers, e.g. make the multiplier of the violet view larger, and the multiplier of the button smaller.
    Then you have an unbroken chain of vertical constraints, and auto layout should work correctly if you assign a height of 0 to your yellow view.