自动布局的高度等于MAX(多视图的高度) [英] Autolayout height equal to MAX(multiple view heights)

查看:130
本文介绍了自动布局的高度等于MAX(多视图的高度)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有一个名为视图容器容器包含5 UIButtons 。我想在容器添加一个高度 NSLayoutConstraint ,而这个高度应等于 NSLayoutHeightAttribute 在其子视图最高的按钮。

Say I have a view called container. container contains 5 UIButtons. I want to add a height NSLayoutConstraint on container, and this height should be equal to the NSLayoutHeightAttribute of the tallest button in its subviews.

我不明白一个简单的方法来做到这一点。任何人有什么想法?

I don't see a straightforward way to do this. Anyone have any ideas?

推荐答案

您需要一个约束每个子视图(按钮),指定容器的高度应大于或等于子视图的高度。把那约束的高优先级,例如 UILayoutPriorityRequired (这是默认反正)。

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).

然后在容器的高度增加一个约束,指定它应该有等于零的高度。把那约束的低优先级,例如 UILayoutPriorityLow 。由于汽车布局试图尽量减少不满意约束错误,就会使容器尽可能短,同时仍然满足所有更高优先级的限制。

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:

蓝色的意见有固定的高度。晒黑的看法是蓝色的意见和上海华正如我上面描述了它的高度限制。我寄托每个子视图的底部到容器的底部,但你可以钉顶部或Y代替中心

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.

这篇关于自动布局的高度等于MAX(多视图的高度)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆