对两个对象之间的居中和对象的约束 [英] Constraints to center and object between two objects

查看:147
本文介绍了对两个对象之间的居中和对象的约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的设计要求按钮在两个其他对象之间居中。其中一个对象位于视图的垂直中心(绿色)。另一个物体距底边有一定距离(绿色)。现在的任务是将第三个对象(红色)置于其他两个对象之间。我正在使用xcode6的新约束,我的视图是在wRegular hRegular模式下。这对于代码来说很容易,但我正在尝试使用故事板来实现这一目标。

My design requires that a button is centered between two other objects. One of the objects is in the vertical center of the view (green). The other object is some distance from the bottom edge (green). Now the task is to center the third object (red) between the other two. I am using xcode6's new constraints and my view is in wRegular hRegular mode. This would be easy with code, but I am trying to use the storyboard to accomplish this.

推荐答案

有很多方法:


  • 在iOS 9中,最简单的方法是定义一个垂直的 UIStackedView ,其中分布的等间距和然后 addArrangedSubview 三个循环子视图

  • In iOS 9, the easiest would be to define a vertical UIStackedView with a distribution of "equal spacing" and then addArrangedSubview the three circular subviews

iOS 9中的另一个选项是创建两个 UILayoutGuide (代表图片中的两个问号),使用 addLayoutGuide 将它们添加到共享超级视图中,并将它们定义为彼此相同的大小。相应的VFL可能如下所示:

Another option in iOS 9 would be to create two UILayoutGuide (which would represent the two question marks in your image), add them to the shared superview with addLayoutGuide and define them to be the same size as each other. The corresponding VFL might look like:

"V:|[greenView1(==100)]-[layoutGuide1]-[redView(==50)]-[layoutGuide2(==layoutGuide1)]-[greenView2(==greenView1)]|"


  • 在早期的iOS版本中,而不是使用 UILayoutGuide ,你可以创建两个空间视图( UIView ,背景清晰,因此它们不可见)然后定义一个约束,使它们的高度相同,然后在五个视图(三个圆圈和两个间隔视图)之间定义垂直间距约束,常量为零。

  • In earlier iOS versions, rather than using UILayoutGuide, you could just create two "space" views (UIView with clear background so they're not visible) then define a constraint such that their heights are identical, and then define vertical spacing constraints between the five views (the three circles and the two spacer views) with a constant of zero.

    它可能看起来像:

    我已经将这些间隔视图显示出来,以说明这个想法,但显然你将它们设置为透明,这样你就无法在UI中看到它们。

    I've made those "spacer" views visible, to illustrate the idea, but obviously you'd set them to be transparent so you can't see them in the UI.

    这在逻辑上等同于iOS 9中的 UILayoutGuide 方法,但 UIView spacer 视图只比 UILayoutGuide 带来更多的开销。但在9之前的iOS版本中,这是解决此问题的常用方法。

    This is logically equivalent to the UILayoutGuide approach in iOS 9, except that the UIView "spacer" views just carry a little more overhead than a UILayoutGuide. But in iOS versions before 9, this is the common approach to this problem.

    这篇关于对两个对象之间的居中和对象的约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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