iOS 6 自动布局约束错误:"Something is nil"



我正在尝试在iOS 6中使用约束,并希望将标签与另一个标签对齐。一个标签和约束是在按钮上的点击中动态创建的。这是一个非常简单的例子,但是我得到有线错误,我不知道原因是什么。

代码:

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

}

错误:

由于未被发现的例外" nsinvalidargumentException"终止应用程序,原因是:'无法在Description forlayOutAttribute_layoutitem_coefffority中创建描述。某事是nil'

当我应用约束时而不是设置限制时发生错误。我已经在

之前设置了一个断点
[self.view addConstraint:cn]

当我检查结果时,我获得了此4

的零值
container, markerAndPositiveExtraVar, negativeExtraVar and _flange
NSLayoutConstraint *cn = [NSLayoutConstraint constraintWithItem:labelLastTime
  attribute:NSLayoutAttributeTop
  relatedBy:NSLayoutRelationEqual
  toItem:self.labelTodayVsLastTime
  attribute:NSLayoutFormatAlignAllBottom
  multiplier:1.0
  constant:5.0];

我的约束定义略有不同,原因是xcode autocomplete snafu。

看看第二个attribute:行。您可能想要attribute: NSLayoutAttributeBottom

NSLayoutFormatAlignAllBottom用于构建一个 NSLayoutFormatOptions bitmask,以给予constraintsWithVisualFormat:options:metrics:views:

相关内容

最新更新