2

我正在尝试定位标签,使标签顶部位于 UIViewController 下方的 2/3 处。所以我写了这个约束,但它给了我下面的错误。

NSLayoutConstraint *labelTopConstraint = [NSLayoutConstraint constraintWithItem:self.myLabel
                                         attribute:NSLayoutAttributeTop
                                         relatedBy:NSLayoutRelationGreaterThanOrEqual
                                            toItem:self.view
                                         attribute:NSLayoutAttributeHeight
                                        multiplier:0.66
                                          constant:0];

错误:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', 
reason: '*** +[NSLayoutConstraint 
constraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant:]: 
Invalid pairing of layout attributes'
4

3 回答 3

7

是的,这很烦人是不是。假设您的视图控制器是屏幕的全高,您可以使用 NSLayoutAttributeBottom 而不是 height,这应该会给您相同的结果。

于 2013-04-21T07:11:09.067 回答
2

您可以添加此约束:

[self.view addConstraints:
   [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(>=topSpace)-[topLabel]"
                                           options:0
                                           metrics:@{@"topSpace":@(self.view.bounds.size.height*0.66f)}
                                             views:NSDictionaryOfVariableBindings(topLabel)]];

我建议您在此viewDidAppear:之前或之后调用它,因为在此之前高度将为零。

于 2013-04-21T10:49:27.530 回答
0

这不正确 - self.myLabel 的顶部应该 >= 0.66 * self.view.height?self.myLabel 的高度是一回事,它的顶部是另一回事。如果您可以将 self.myLabel 的底部边缘固定到超级视图的底部边缘,那么我认为如果您设置这样的约束:

NSLayoutConstraint *labelHeightConstraint = [NSLayoutConstraint constraintWithItem:self.myLabel
                                         attribute:NSLayoutAttributeHeight
                                         relatedBy:NSLayoutRelationGreaterThanOrEqual
                                            toItem:self.view
                                         attribute:NSLayoutAttributeHeight
                                        multiplier:0.66
                                          constant:0];

这应该确保 self.myLabel 的高度始终至少是根视图高度的 2/3。

于 2013-04-21T21:55:32.583 回答