简体   繁体   English

Xcode Interface Builder:使视图的中心X等于其超级视图的三分之一

[英]Xcode Interface Builder: Make a view's center X equal to third of its superview

I have a two views I'd like to place in a way that they are equally spaced horizontally. 我有两个视图,我想以一种水平相等的方式放置。

I'd like to define thier Center X so that they are the third and two-thirds of the width of the superview but I have found no constraint setting that allows you to do that. 我想定义它们的Center X使它们是超视图宽度第三和三分之二,但我没有找到允许你这样做的约束设置。

Can you tell me how can this be achieved? 你能告诉我怎么能实现这个目标?

在此输入图像描述

You can use a Center X constraint with the following settings: 您可以使用具有以下设置的Center X约束:

中心X.

For the second view the Multiplier has to be 2/3 of course. 对于第二种观点, Multiplier必须是2/3

Result: 结果: 结果

Select Align Center X from the subView and the Superview (screen of only the last one) 从子视图和超级视图中选择对齐中心X(仅显示最后一个的屏幕) 在此输入图像描述

Change the multiplier: 2:3 for the first one, and 4:3 for the second one: 更改乘数:第一个为2:3,第二个为4:3:

在此输入图像描述

You said: 你说:

I have a two views I'd like to place in a way that they are equally spaced horizontally. 我有两个视图,我想以一种水平相等的方式放置。

I'd like to define their Center X so that they are the third and two-thirds of the width of the superview but I have found no constraint setting that allows you to do that. 我想定义他们的中心X,使它们是超视图宽度的第三和三分之二,但我发现没有允许你这样做的约束设置。

It's worth noting that "equally spaced" and "center x that is ⅓ and ⅔ of the superview" are not actually the same thing. 值得注意的是,“等间距”和“中心x是超视图的⅓和”“实际上并不是同一个东西。 Consider the "⅓ and ⅔" scenario: Imagine a superview that is 300 pixels wide, which would put the ⅓ and ⅔ points at 100 and 200 respectively. 考虑“⅓和⅔”场景:想象一个300像素宽的超视图,它将⅓和⅔点分别设置为100和200。 If you then add two subviews that are 50 pixels wide, then the left and right margins will be 75 pixels each, but the center margin will be 50 pixels. 如果然后添加两个50像素宽的子视图,则左边距和右边距各为75像素,但中间边距为50像素。 If the subviews are wider, it gets worse (eg if the subviews were 100 pixels wide, there would be 50 pixel margins left and right, but absolutely no space in between). 如果子视图更宽,则会变得更糟(例如,如果子视图宽度为100像素,则左右会有50个像素边距,但两者之间绝对没有空间)。

If you really want them evenly spaced (where the horizontal space between the subviews is the same as the space between the subviews and the edges of their superview), you can use layout guides if doing it programmatically. 如果您确实希望它们均匀间隔(子视图之间的水平空间与子视图之间的空间和超视图的边缘相同),则可以使用布局指南(如果以编程方式执行)。

So, assuming you've already created two subviews and have taken care of their size and vertical constraints, you can then do: 因此,假设您已经创建了两个子视图并且已经处理了它们的大小和垂直约束,那么您可以执行以下操作:

// create three layout guides

let layoutGuides = (0 ..< 3).map { _ in UILayoutGuide() }

// add them to your view

layoutGuides.forEach { view.addLayoutGuide($0) }

// create dictionary for VFL

let views = ["layoutGuide0": layoutGuides[0], "layoutGuide1": layoutGuides[1], "layoutGuide2": layoutGuides[2], "subview0": subviews[0], "subview1": subviews[1]]

// define horizontal constraints where three layout guides are the same width

let vfl = "H:|[layoutGuide0][subview0][layoutGuide1(==layoutGuide0)][subview1][layoutGuide2(==layoutGuide0)]|"

let constraints = NSLayoutConstraint.constraints(withVisualFormat: vfl, metrics: nil, views: views)
NSLayoutConstraint.activate(constraints)

If you wanted to do this in IB, you can't create these sorts of layout guides, but you can add three invisible "spacer" views (ie simple UIView objects with alpha of 0) with constraints that keep their respective widths equal to each other. 如果你想做到这一点在IB,您不能创建这些类型的布局导向的,但你可以添加三个看不见的“间隔”的观点(即简单UIView与保持各自的宽度相等的约束以0阿尔法对象)其他。 Then stick these five views (the two subviews plus the three spacer views) in stack view (or, if you are a masochist, bypass stack view and add the six leading/trailing constraints in between these five subviews yourself) and you achieve the desired effect of two evenly spaced subviews (in between these three non-visible "spacer" views). 然后在堆栈视图中粘贴这五个视图(两个子视图加上三个间隔视图)(或者,如果您是受虐狂,绕过堆栈视图并在这五个子视图之间添加六个前导/尾随约束)并实现所需两个均匀间隔的子视图的效果(在这三个不可见的“间隔”视图之间)。

So, this is what it looks like (making the three "spacer" views visible so you can see what's going on): 所以,这就是它的样子(让三个“间隔”视图可见,这样你就可以看到发生了什么):

在此输入图像描述

The net effect, when you give those three spacer views an alpha of 0 (to make them invisible) is as follows: 最终的效果,当你给这三个间隔意见alpha的0(以使其不可见)如下:

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM