Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS 9 constraint error

This code:

[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]];

Work's fine on iOS 8 and iOS7 but now when with iOS9 when I run my app I get this error :

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** +[NSLayoutConstraint constraintWithItem:attribute:relatedBy:toItem:attribute:multiplier:constant:]: A constraint cannot be made between a leading/trailing attribute and a right/left attribute. Use leading/trailing for both or neither.'

When I remove those constraints my app works fine. So I need to know what's the problem and what have been changed from iOS8 to iOS9.

like image 820
Chlebta Avatar asked Sep 21 '15 10:09

Chlebta


2 Answers

Your log clearly suggest that...either you can use leading/trailing or right/left not both ...

So just change 4th constraint like below

[self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.collectionView
                                                  attribute:NSLayoutAttributeTrailing 
                                                  relatedBy:NSLayoutRelationEqual 
                                                     toItem:self.view
                                                  attribute:NSLayoutAttributeTrailing
                                                 multiplier:1.0f 
                                                   constant:0.0f]]; 
like image 170
EI Captain v2.0 Avatar answered Sep 20 '22 15:09

EI Captain v2.0


I had these lines:

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];

I changed to this:

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];

This is the solution to the error.

like image 34
delarcomarta Avatar answered Sep 21 '22 15:09

delarcomarta