2012-12-26 51 views
1

我想在iOS 6中使用约束,并希望将标签与另一个对齐。一个标签和约束是在一个按钮上点击动态创建的。这是一个非常简单的例子,但我得到一个有线错误,我不知道是什么原因。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',原因:“无法在descriptionForLayoutAttribute_layoutItem_coefficient创建描述。东西是零'

当我应用约束,而不是当我设置它时发生错误。我已经设置断点之前

[self.view addConstraint:cn] 

,当我检查的结果,我得到零值,这4

container, markerAndPositiveExtraVar, negativeExtraVar and _flange 
+0

你检查过以确保self.labelTodayVsLastTime有效吗?我*想*第二个属性是系数? – Padin215

回答

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

我有同样的问题用一个稍微不同的约束定义和原因是一个Xcode自动完成snafu。

看看第二个attribute:一行。你可能想要像attribute: NSLayoutAttributeBottom这样的东西。

NSLayoutFormatAlignAllBottom用于构建NSLayoutFormatOptions位掩码给constraintsWithVisualFormat:options:metrics:views:之类的东西。

+0

谢谢我前一段时间解决它,但这是正确的答案:) –