使用约束使两个 TableView 保持相同的高度 &相距相同的距离 [英] Using Constraints To Keep Two TableViews The Same Height & The Same Distance Apart

查看:38
本文介绍了使用约束使两个 TableView 保持相同的高度 &相距相同的距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景/我想做的事情

  • 我正在为 iOS 7 构建这个
  • 我正在使用故事板
  • 我有两个 UITableViews,我想保持相同的高度和距离,以保持 3.5 & 之间的视觉一致性4 英寸设备
  • 从 4 英寸屏幕到 3.5 英寸屏幕的转变,我想要:
  • 表格视图变短但高度保持不变
  • 为了使顶部表格视图保持相同的 y 位置
  • 为了使底部表格视图向上移动其 y 位置,以保持顶部表格视图底部和底部表格视图顶部之间的距离相同.他们之间也有同样的差距
  • I'm building this for iOS 7
  • I'm using a StoryBoard
  • I have two UITableViews, that I'd like to keep the same height and distance apart, to retain visual consistency between 3.5 & 4 inch devices
  • So on the shift from a 4 inch screen to 3.5 inch screen I'd like for:
  • The table views to get shorter but remain equal in height
  • For the top table view to keep the same y pos
  • For the bottom table view to shift its y pos up to maintain the same distance between the bottom of the top table view and the top of the bottom table view. AKA the same gap between them

我的尝试

  • 使用故事板中的 Pin 图标,我选择了两个 TableView 并将它们的高度设置为相等,并将此代码包含在视图控制器中:

[self.topTableView addConstraint:[NSLayoutConstraintconstraintWithItem:self.bottomTableView属性:NSLayoutAttributeBottom relatedBy:0toItem:self.bottomTableView 属性:NSLayoutAttributeTop乘数:1 常数:30]];

[self.topTableView addConstraint:[NSLayoutConstraint constraintWithItem:self.bottomTableView attribute:NSLayoutAttributeBottom relatedBy:0 toItem:self.bottomTableView attribute:NSLayoutAttributeTop multiplier:1 constant:30]];

问题

  • 如何在保持相同距离的同时使它们保持相同的高度

更新

推荐答案

查看 Apple 建议的方法(在标题为在视图之间创建相等间距"的部分).

Check out the Apple suggested way to do this (it's in the section titled "Creating Equal Spacing Between Views").

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.

因此,如果您发现自己想要以某种方式相互关联的约束,请查看高度和宽度而不是空间.在您的特定示例中,您可以用隐藏的 UIViewUITableView 包围,将它们的高度设置为相等,并将它们与邻居/容器的空间设置为 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 UITableViews with hidden UIViews, 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 保持相同的高度 &相距相同的距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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