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

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

问题描述

背景/我想做什么


  • 我正在为iOS 7构建此

  • 我正在使用StoryBoard

  • 我有两个 UITableViews ,我想要保持相同的高度和
    的距离,以保持3.5&之间的视觉一致性。 4英寸设备

  • 因此,从4英寸屏幕转换到3.5英寸屏幕我想要:

  • 表格视图缩短但保持高度相等

  • 顶部表格视图保持相同y pos

  • 对于底部表格视图,将其y pos移至保持顶部表视图底部和底部表视图顶部之间的距离相同。 AKA之间的差距相同

  • 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:[NSLayoutConstraint
constraintWithItem:self.bottomTableView
attribute:NSLayoutAttributeBottom relatedBy:0
toItem: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.

所以如果你发现自己想要以某种方式相互关联的约束,看高度和宽度而不是空格。在您的特定示例中,您可以使用隐藏的 UIView 包围 UITableView s,将它们的高度设置为相等,并且他们到邻居/容器的空间 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.

这篇关于使用约束来保持两个TableViews相同的高度&同一距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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