Search code examples
ios6autolayoutnslayoutconstraint

iOS 6 Auto Layout Constraints Error: "Something is nil"


I am trying to use constrains in iOS 6 and want to align a label to another. One label and the constrain are created dynamically on a tap on a button. This is a very simple example but i get a wired error and i don't know what's the cause.

The code:

- (IBAction)addToLog:(UIButton *)sender {
UILabel *labelLastTime = [[UILabel alloc]initWithFrame:CGRectMake(20, 359, 92, 42)];
labelLastTime.text = [NSString stringWithFormat:@"%@kg x %@", self.selectedPickerWeight, self.selectedPickerRepetitions];

labelLastTime.font = [UIFont boldSystemFontOfSize:17.0];
labelLastTime.textAlignment = NSTextAlignmentCenter;
labelLastTime.textColor = [UIColor colorWithRed:133.0/255.0 green:133.0/255.0 blue:133.0/255.0 alpha:1.0];

labelLastTime.backgroundColor = [UIColor colorWithRed:228.0/255.0 green:228.0/255.0 blue:228.0/255.0 alpha:1.0];
labelLastTime.layer.borderColor = [UIColor colorWithRed:185.0/255.0 green:185.0/255.0 blue:185.0/255.0 alpha:1.0].CGColor;
labelLastTime.layer.borderWidth = 1;
labelLastTime.layer.cornerRadius = 5;

[self.view setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.view addSubview:labelLastTime];


NSLayoutConstraint *cn = [NSLayoutConstraint constraintWithItem:labelLastTime
                                                       attribute:NSLayoutAttributeTop
                                                       relatedBy:NSLayoutRelationEqual
                                                          toItem:self.labelTodayVsLastTime
                                                       attribute:NSLayoutFormatAlignAllBottom
                                                      multiplier:1.0
                                                        constant:5.0];

[self.view addConstraint:cn];

}

The Error:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unable to create description in descriptionForLayoutAttribute_layoutItem_coefficient. Something is nil'

The error occurs when i apply the constrain, not when i set it up. I have set a breakpoint just before

[self.view addConstraint:cn]

and when i inspect the result, i get nil values for this 4

container, markerAndPositiveExtraVar, negativeExtraVar and _flange

Solution

  • NSLayoutConstraint *cn = [NSLayoutConstraint constraintWithItem:labelLastTime
      attribute:NSLayoutAttributeTop
      relatedBy:NSLayoutRelationEqual
      toItem:self.labelTodayVsLastTime
      attribute:NSLayoutFormatAlignAllBottom
      multiplier:1.0
      constant:5.0];
    

    I had the same problem with a slightly different constraint definition and the cause was an Xcode autocomplete snafu.

    Have a look at the second attribute: line. You probably wanted something like attribute: NSLayoutAttributeBottom.

    NSLayoutFormatAlignAllBottom is for building an NSLayoutFormatOptions bitmask to be given to something like constraintsWithVisualFormat:options:metrics:views:.