约束崩溃

时间:2014-07-03 15:29:06

标签: ios xcode constraints

我创建了两个约束来保持我的标签在3.5和#34; iPhone和4"苹果手机。我已经在代码中完成了它:

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 216, 320, 137)];
UIFont *font = [UIFont fontWithName:@"HelveticaNeue-UltraLight" size:size];
[label setFont:font];
[label setBackgroundColor:[UIColor blackColor]];
[label setAlpha:0];
[label setTextAlignment:NSTextAlignmentCenter];
[label setTextColor:[UIColor whiteColor]];
[label setText:text];
self.alertLabel = label;
[self.timeLabel setHidden:YES];
NSLayoutConstraint *centerLayoutY = [NSLayoutConstraint constraintWithItem:self.view attribute:self.view.center.y relatedBy:NSLayoutRelationEqual toItem:label attribute:label.center.y multiplier:1 constant:0];
NSLayoutConstraint *centerLayoutX = [NSLayoutConstraint constraintWithItem:self.view attribute:self.view.center.x relatedBy:NSLayoutRelationEqual toItem:label attribute:label.center.x multiplier:1 constant:0];
[self.view addConstraint:centerLayoutY];
[self.view addConstraint:centerLayoutX];
[self.view addSubview:label];

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:1.0];
[label setAlpha:0.6f];
[UIView commitAnimations];

当我运行应用程序时,它会崩溃:

2014-07-03 18:24:12.361 NigthLigth[2550:60b] The view hierarchy is not prepared for the constraint: <NSLayoutConstraint:0x8f544d0 H:[UILabel:0x8f533c0]-(0)-[UIView:0x8c57d30]>
When added to a view, the constraint's items must be descendants of that view (or the view itself). This will crash if the constraint needs to be resolved before the view hierarchy is assembled. Break on -[UIView _viewHierarchyUnpreparedForConstraint:] to debug.
2014-07-03 18:24:12.362 NigthLigth[2550:60b] View hierarchy unprepared for constraint.
Constraint: <NSLayoutConstraint:0x8f544d0 H:[UILabel:0x8f533c0]-(0)-[UIView:0x8c57d30]>
Container hierarchy: 
<UIView: 0x8c57d30; frame = (0 0; 320 480); autoresize = RM+BM; gestureRecognizers = <NSArray: 0x8e558a0>; layer = <CALayer: 0x8c57f10>>
   | <UIImageView: 0x8c580c0; frame = (0 -44; 320 568); autoresize = W+H; userInteractionEnabled = NO; layer = <CALayer: 0x8c581c0>>
   | <UILabel: 0x8c5af80; frame = (34.5 182.5; 251 115); text = '06:24'; clipsToBounds = YES; hidden = YES; opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <CALayer: 0x8c5b0b0>>
   | <_UILayoutGuide: 0x8e6ccb0; frame = (0 0; 0 0); hidden = YES; layer = <CALayer: 0x8e6cdc0>>
   | <_UILayoutGuide: 0x8e6d240; frame = (0 480; 0 0); hidden = YES; layer = <CALayer: 0x8e6d2b0>>
View not found in container hierarchy: <UILabel: 0x8f533c0; frame = (0 216; 320 137); text = 'Cannot Snooze'; alpha = 0; userInteractionEnabled = NO; layer = <CALayer: 0x8f42870>>
That view's superview: NO SUPERVIEW
2014-07-03 18:24:12.372 NigthLigth[2550:60b] *** Terminating app due to uncaught exception 'NSGenericException', reason: 'Unable to install constraint on view.  Does the constraint reference something from outside the subtree of the view?  That's illegal. constraint:<NSLayoutConstraint:0x8f544d0 H:[UILabel:0x8f533c0]-(0)-[UIView:0x8c57d30]> view:<UIView: 0x8c57d30; frame = (0 0; 320 480); autoresize = RM+BM; gestureRecognizers = <NSArray: 0x8e558a0>; layer = <CALayer: 0x8c57f10>>'

错在哪里?

1 个答案:

答案 0 :(得分:0)

你做错了几件事。首先,你应该只使用带有标签的alloc init,你不应该设置一个框架。您还需要将标签的translatesAutoresizingMaskIntoConstraints属性设置为NO。其次,在添加约束之前,您需要将子视图添加到其超级视图中。第三,属性参数不应该像&#34; label.center.y&#34;,它应该是NSLayoutAttribute类型之一,如NSLayoutAttributeCenterY。