简体   繁体   English

锁定在左右边缘的视图可能会随着高度的变化而变化……我该如何加以约束?

[英]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.

相关问题 如何使SnapKit约束左右边缘? - How can I make SnapKit constrain the left and right edges? 如何以编程方式将此背景限制在屏幕边缘 - How do I constrain this background to the edges of the screen programmatically 如何将 SKShapeNode 约束到 SpriteKit 中的设备边缘? - How do I constrain an SKShapeNode to the device edges in SpriteKit? 如何将视图约束到UINavigationBar的左下角? - How to constrain a view to the bottom left of a UINavigationBar? 如何将视图固定到屏幕边缘? - How do I pin a view to the edges of a screen? 如何将UIStackView约束到其父视图的中心? - How do I constrain a UIStackView to the center of its parent view? iOS-如何设置约束,以使标签的底部等于图像视图,但高度可以变化 - iOS - How to set constraints such that the bottom of a label will be equal to an image view but the height can vary 如何限制导航按钮一直向左或向右? (iOS) - How to constrain navigation buttons all the way to the left or right? (iOS) 如何调整滚动视图的大小,以使所有边缘与容器视图的边缘齐平? - How do I Resize the scroll view so that all of the edges are flush with the container view’s edges? iOS开发:如何使视图向右过渡而不是默认向左过渡? - iOS Development: How can I make my view transition to the right instead of to the default left?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM