iOS 9约束错误

时间:2015-09-21 10:25:14

标签: ios objective-c cocoa-touch

此代码:

[self.collectionView setTranslatesAutoresizingMaskIntoConstraints: NO];

[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.collectionView
                                                      attribute:NSLayoutAttributeTop
                                                      relatedBy:NSLayoutRelationEqual
                                                         toItem:self.topLayoutGuide
                                                      attribute:NSLayoutAttributeTop
                                                     multiplier:1.0f
                                                       constant:1.0f]];

[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.collectionView
                                                      attribute:NSLayoutAttributeBottom
                                                      relatedBy:NSLayoutRelationEqual
                                                         toItem:self.bottomLayoutGuide
                                                      attribute:NSLayoutAttributeBottom
                                                     multiplier:1.0f 
                                                       constant:0.f]];

[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.collectionView 
                                                      attribute:NSLayoutAttributeLeading 
                                                      relatedBy:NSLayoutRelationEqual 
                                                         toItem:self.view
                                                      attribute:NSLayoutAttributeLeading
                                                     multiplier:1.0f
                                                       constant:0.0f]];

[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.collectionView
                                                      attribute:NSLayoutAttributeTrailing 
                                                      relatedBy:NSLayoutRelationEqual 
                                                         toItem:self.view
                                                      attribute:NSLayoutAttributeRight
                                                     multiplier:1.0f 
                                                       constant:0.0f]];

在iOS 8和iOS7上工作正常,但现在当我运行我的应用程序时使用iOS9时出现此错误:

  

由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'*** + [NSLayoutConstraint constraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant:]:无法在前导/尾随属性和右/左属性。对两者使用前导/尾随或两者都不使用。'

当我删除这些约束时,我的应用程序正常工作。所以我需要知道问题是什么以及从iOS8到iOS9的变化是什么。

2 个答案:

答案 0 :(得分:14)

您的日志清楚地表明......您可以使用leading/trailingright/left而不是两者......

所以只需更改下面的第4个约束

[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.collectionView
                                                  attribute:NSLayoutAttributeTrailing 
                                                  relatedBy:NSLayoutRelationEqual 
                                                     toItem:self.view
                                                  attribute:NSLayoutAttributeTrailing
                                                 multiplier:1.0f 
                                                   constant:0.0f]]; 

答案 1 :(得分:2)

我有这些线:

NSLayoutConstraint *leftConstraint = [NSLayoutConstraint constraintWithItem:self.contentView
                                                                  attribute:NSLayoutAttributeLeading
                                                                  relatedBy:0
                                                                     toItem:self.view
                                                                  attribute:NSLayoutAttributeLeft
                                                                 multiplier:1.0
                                                                   constant:0];
[self.view addConstraint:leftConstraint];

NSLayoutConstraint *rightConstraint = [NSLayoutConstraint constraintWithItem:self.contentView
                                                                   attribute:NSLayoutAttributeTrailing
                                                                   relatedBy:0
                                                                      toItem:self.view
                                                                   attribute:NSLayoutAttributeRight
                                                                  multiplier:1.0
                                                                    constant:0];
[self.view addConstraint:rightConstraint];

我改为:

NSLayoutConstraint *leftConstraint = [NSLayoutConstraint constraintWithItem:self.contentView
                                                                  attribute:NSLayoutAttributeLeading
                                                                  relatedBy:0
                                                                     toItem:self.view
                                                                  attribute:NSLayoutAttributeLeading
                                                                 multiplier:1.0
                                                                   constant:0];
[self.view addConstraint:leftConstraint];

NSLayoutConstraint *rightConstraint = [NSLayoutConstraint constraintWithItem:self.contentView
                                                                   attribute:NSLayoutAttributeTrailing
                                                                   relatedBy:0
                                                                      toItem:self.view
                                                                   attribute:NSLayoutAttributeTrailing
                                                                  multiplier:1.0
                                                                    constant:0];
[self.view addConstraint:rightConstraint];

这是错误的解决方案。