1

iOS 6 で制約を使用しようとしていますが、ラベルを別のラベルに合わせたいと考えています。ボタンをタップすると、1 つのラベルと制約が動的に作成されます。これは非常に単純な例ですが、有線エラーが発生し、原因がわかりません。

コード:

- (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 に対して nil 値が得られます

container, markerAndPositiveExtraVar, negativeExtraVar and _flange
4

1 に答える 1

2
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:.

于 2013-06-13T12:55:11.203 に答える