J'ai créé deux contraintes pour garder mon label au centre à la fois sur l'iPhone de 3,5 "et l'iPhone de 4". Je l'ai fait dans le code:Contraintes Crash
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];
Quand je lance l'application, il se bloque:
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>>'
Qu'est-ce qui ne va pas?
Pourquoi ne pas utiliser IB? Pensez à tout le codage que vous économiserez! Et pourquoi utilisez-vous des animations de style iOS 4.0? –