简体   繁体   English

界面生成器约束

[英]Interface Builder Constraints

I'm trying to create layout like this in interface builder. 我正在尝试在界面生成器中创建这样的布局。

在此处输入图片说明

I'm using constraints to make these views stretchable. 我正在使用约束使这些视图可拉伸。 Top view has left, right and top space bound to superview, and bottom space to bottom view. 顶视图具有绑定到超级视图的左,右和顶部空间,而底视图则具有底部空间。 Bottom view has the left right and bottom space bound to superview. 底部视图具有绑定到超级视图的左右空间。

On runtime I add ViewController views to both of them. 在运行时,我向它们两个都添加了ViewController视图。

Issue here is that there is no constraint for Y of bottom view, IB shows red error arrow and so on. 这里的问题是底视图的Y没有约束,IB显示红色错误箭头,依此类推。 That is because I don't know exact height of it. 那是因为我不知道它的确切高度。 Is there any "android wrap_content" constraint for yellow view to be with height which is equal to it's inner view added in runtime? 黄色视图的高度是否等于运行时添加的内部视图,是否存在任何“ android wrap_content”约束?

Add a constraint for the height of the yellow view. 为黄色视图的高度添加约束。 Then add an IBOutlet for that constraint so you can make changes to the constraint's constant value at runtime. 然后为该约束添加IBOutlet,以便您可以在运行时更改约束的constant量值。 If you need to calculate the size that satisfies the yellow view's constraints, you can use systemLayoutSizeFittingSize: . 如果您需要计算满足黄色视图约束的尺寸,则可以使用systemLayoutSizeFittingSize:

Alternatively, you can rely on the yellow view's intrinsicContentSize . 另外,您可以依赖黄色视图的intrinsicContentSize Add a placeholder Intrinsic Size for the view in Interface Builder. 在Interface Builder中为视图添加一个占位符“本征大小”。 If your yellow view implements intrinsicContentSize or has constraints that give it an unambiguous height (eg if it contains a UILabel or UIImageView that is pinned at the top and the bottom), then that will be enough to appropriately size the view. 如果您的黄色视图实现了intrinsicContentSize或具有使其具有明确高度的约束(例如,如果它包含固定在顶部和底部的UILabelUIImageView ),则足以适当调整视图的大小。

Just add height constraint to yellow view, for example: 只需将高度限制添加到黄色视图,例如:

NSLayoutConstraint *heightConstraint = [NSLayoutConstraint constraintWithItem:self.yellowView
                                                                        attribute:NSLayoutAttributeHeight
                                                                        relatedBy:NSLayoutRelationEqual
                                                                           toItem:self.view
                                                                        attribute:NSLayoutAttributeHeight
                                                                       multiplier:0.20f
                                                                         constant:0.0f];
heightConstraint.active = YES;

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

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