[英]A view locked on the left and right edges that can vary in height… how do I constrain that?
I am new to interface builder. 我是界面构建者的新手。
I want to create a view that is centered horizontally and vertically. 我想创建一个水平和垂直居中的视图。 The view has to be the width of the screen but the height can vary slightly.
视图必须是屏幕的宽度,但高度可以略有不同。 How do I constrain that view?
我如何约束这种看法?
I have added trailing and leading space to superview, to lock the right and left edges to the screen border. 我添加了尾部和前导空间来进行超级视图,以将左右边缘锁定到屏幕边框。 I have added center Y to superview.
我已将中心Y添加到超级视图中。
Xcode whines "need constrain for Y position or height". Xcode抱怨“需要限制Y位置或高度”。 As far as I know a constrain I have added to center Y on superview is exactly that.
据我所知,我在超级视图上添加到中心Y的约束就是这样。 How do I solve that?
我该如何解决?
You always need two constraints for X, and two constraints for Y. The constraints determine position and size . 你总是需要对X 两个约束条件,并为Y. 两个约束约束确定位置和大小 。 You have both constraints for X, but only a position constraint for Y. You need an additional constraint for Y that determines the vertical size of the view.
您对X都有两个约束,但是对于Y只有一个位置约束。您需要对Y附加约束,该约束确定视图的垂直大小 。
When using auto layout, you don't programmatically change the height of a view. 使用自动布局时,无需以编程方式更改视图的高度。 Instead, you programmatically change the constraint that determines the height of the view.
相反,您可以通过编程方式更改确定视图高度的约束。 It's a level of indirection that's confusing at first (and annoying forever), but that's the way auto layout works.
首先,这是一个令人困惑的间接级别(并且永远令人烦恼),但这就是自动布局的工作方式。
You can control-drag from a constraint in storyboard into the code to create an IBOutlet for the constraint, just like you would create an IBOutlet for the view itself. 您可以控制从情节提要中的约束拖动到代码中,以为约束创建IBOutlet,就像为视图本身创建IBOutlet一样。 Then you can change the properties of the constraint (eg the
constant
property) at runtime to change the height of the view. 然后,您可以在运行时更改约束的属性(例如,
constant
属性)以更改视图的高度。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.