Autolayout height equal to MAX(multiple view heights)

后端 未结 1 525
故里飘歌
故里飘歌 2021-02-02 09:51

Say I have a view called container. container contains 5 UIButtons. I want to add a height NSLayoutConstraint on contai

相关标签:
1条回答
  • 2021-02-02 10:06

    You need one constraint for each subview (button), specifying that the container's height should be greater than or equal to the subview's height. Give that constraint a high priority, like UILayoutPriorityRequired (which is the default anyway).

    Then add one more constraint on the container's height, specifying that it should have a height equal to zero. Give that constraint a low priority, like UILayoutPriorityLow. Since auto layout tries to minimize the error of unsatisfied constraints, it will make the container as short as possible while still satisfying all higher-priority constraints.

    I have put an example in this gist. It produces this result:

    The blue views have fixed heights. The tan view is the superview of the blue views and its height is constrained as I described above. I pinned each subview's bottom to the container's bottom, but you could pin the tops or the Y centers instead.

    0 讨论(0)
提交回复
热议问题