自动布局:以编程方式将视图顶部固定到另一个视图的底部

时间:2014-10-07 19:03:36

标签: ios autolayout constraints

我正在将我的XIB文件切换到自动布局并遇到这个问题我似乎无法在堆栈上找到一个好的答案。

我希望以编程方式将一个视图的顶部固定到另一个视图的底部。基本上,俯视图向下推动底部视图,导致底部视图的高度按比例减小,同时保持固定在底部。

顶视图是固定高度,而底视图是可变的。在伪代码中,我基本上想要这个:

If the top view is present, pin the top portion of the bottom view, to the bottom of the top view while being anchored at the bottom (or reduce height to compensate for the top view)

Otherwise, the bottom view will be displayed at full height, essentially height = top view + reduced height bottom view 

这需要以编程方式完成,因为顶视图只是有条件地显示,否则我希望底部视图占据顶视图引入的高度。

希望这是有道理的!

1 个答案:

答案 0 :(得分:3)

如果您拥有topView视图的媒体资源bottomViewself,则可以执行以下操作:

[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[_topView]-[_bottomView]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_topView, _bottomView)]];

将您的顶视图高度设置为零也可以回答您的隐藏问题。

相关问题