使用iPad视图处理自动布局

时间:2016-02-22 17:17:23

标签: ios autolayout

我想根据视图高度以编程方式设置我的约束。例如,我想将视图中的约束设置为等于(self.view.height)* 0.05。但是,据我所知,这是不可能的,如果你试图做那个应用程序会引发异常。

我使用砌体(用于以编程方式帮助轻松设置约束的库),我找到答案:

you cannot relate size attributes (height, width) to position attributes (left, top etc)

If you wanted 20% margin on both top and bottom you could create a constraint which is

make.height.equalTo(superview.mas_height).multipliedBy(60/100.0);
make.center.equalTo(superview.mas_center);

有没有办法达到这一点?感谢。

编辑:

例如,该代码将引发异常:

[imageNewsLabel mas_makeConstraints:^(MASConstraintMaker *make) {

        make.bottom.equalTo(self.view.mas_height)multiplyBy(0.2);

它与典型的以编程方式添加的约束语法不同,但我希望它可以放弃显而易见的理解。 That will raise an exception: invalid pairing of layout attributes.

1 个答案:

答案 0 :(得分:1)

您需要添加一个间隔视图,它将位于视图和其父级边缘之间。如果要保持与iOS 8及更低版本的兼容性,请使用常规视图(空,透明),并使用您选择的乘数约束其高度。如果您只定位iOS 9及更高版本,则可以使用UILayoutGuide执行相同操作但不需要UIView的费用。