问题描述
Apple 现在倾向于为此目的使用堆栈视图.请参阅此 WWDC 2013 视频(适用于 OS X) 和 这个来自 WWDC 2015 的 iOS
他们不提供仅限约束的解决方案.相反,他们建议在可见视图之间放置隐藏的间隔视图".
They don't present a constraints-only solution. Instead, they suggest placing hidden "spacer views" between your visible views.
这背后的原因源于约束系统的工作方式.约束关系始终严格存在于两个对象之间.在间距约束中,这种关系必须在被隔开的东西和它的 X
间距的东西之间.就是这样.没有空间让第三个关系使空间(本身是关系的结果)等于某个其他值.
The reasoning behind this is rooted in the way the constraint system works. Constraint relationships are always strictly between two objects. In a spacing constraint, that relationship must be between the thing that's being spaced, and the thing it's X
space from. That's it. There's no room for a third relation to make the space (itself the result of a relation) equal to some other value.
另一方面,高度(和宽度)约束不是关系.您可以使用与其他任何事物无关的简单常量来设置事物的高度.因此,有空间"可以指定与另一个对象的关系——例如,高度应该等于另一个高度(或宽度,或其他).
Height (and width) constraints, on the other hand, are not relations. You can set the height of a thing with a simple constant that's unrelated to anything else. Thus there's "room" to specify a relationship to another object — that a height should be equal to another height (or width, or whatever), for example.
因此,如果您发现自己想要以某种方式相互关联的约束,请查看高度和宽度而不是空间.在您的特定示例中,您可以用隐藏的 UIView
将 UITableView
包围,将它们的高度设置为相等,并将它们与邻居/容器的空间设置为 0
或其他一些低常数.
So if you ever find yourself wanting constraints that are all related in some way to each other, look to heights and widths instead of spaces. In your particular example, you can surround your UITableView
s with hidden UIView
s, set their heights to be equal, and their space to neighbors/containers to 0
or some other low constant.
作为额外的奖励,这在 IB 中都是可能的.无需自定义代码或子类.
As an added bonus, this is all possible in IB. No need for custom code or subclasses.
这篇关于使用约束使两个 TableView 保持相同的高度 &相距相同的距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!