0

My code is very simple. I need to stack my view one after another. Adding both view in self.view with 20 px space

- (void)viewDidLoad {
    [super viewDidLoad];
    UILabel *label = [[UILabel alloc] init];
    [label setTranslatesAutoresizingMaskIntoConstraints:NO];
    [label.layer setBorderWidth:1.f];
    [label.layer setBorderColor:[[UIColor grayColor] CGColor]];
    [label setText:@"label1"];
    [self.view addSubview:label];
    NSLayoutConstraint *yConstraint =[NSLayoutConstraint
                                      constraintWithItem:label
                                      attribute:NSLayoutAttributeTop
                                      relatedBy:NSLayoutRelationEqual
                                      toItem:label.superview
                                      attribute:NSLayoutAttributeTop
                                      multiplier:1.f
                                      constant:20];
    [label.superview addConstraint:yConstraint];
    NSLayoutConstraint *width =[NSLayoutConstraint
                                constraintWithItem:label
                                attribute:NSLayoutAttributeWidth
                                relatedBy:NSLayoutRelationEqual
                                toItem:label.superview
                                attribute:NSLayoutAttributeWidth
                                multiplier:1.f
                                constant:0];
    [label.superview addConstraint:width];
    NSLayoutConstraint *centerX =[NSLayoutConstraint
                                  constraintWithItem:label
                                  attribute:NSLayoutAttributeCenterX
                                  relatedBy:NSLayoutRelationEqual
                                  toItem:label.superview
                                  attribute:NSLayoutAttributeCenterX
                                  multiplier:1.0
                                  constant:0];
    [label.superview addConstraint:centerX];

    NSLayoutConstraint *height =[NSLayoutConstraint
                                 constraintWithItem:label
                                 attribute:NSLayoutAttributeHeight
                                 relatedBy:NSLayoutRelationEqual
                                 toItem:nil
                                 attribute:NSLayoutAttributeNotAnAttribute
                                 multiplier:1.0
                                 constant:40];
    [label addConstraint:height];

    UILabel *labe2 = [[UILabel alloc] init];
    [labe2 setTranslatesAutoresizingMaskIntoConstraints:NO];
    [labe2 setText:@"label2"];
    [self.view addSubview:labe2];

    yConstraint =[NSLayoutConstraint
                                      constraintWithItem:labe2
                                      attribute:NSLayoutAttributeTop
                                      relatedBy:NSLayoutRelationEqual
                                      toItem:label
                                      attribute:NSLayoutAttributeBottom
                                      multiplier:1.f
                                      constant:20];
    [labe2 addConstraint:yConstraint]; //Crashes here 


}

With message

2015-12-17 12:59:40.313 Testcase[16625:14518988] The view hierarchy is not prepared for the constraint: <NSLayoutConstraint:0x7ae4f990 V:[UILabel:0x7ae4ac30'label1']-(20)-[UILabel:0x7ae4f610'label2']>
    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(UIConstraintBasedLayout) _viewHierarchyUnpreparedForConstraint:] to debug.
2015-12-17 12:59:40.315 Testcase[16625:14518988] View hierarchy unprepared for constraint.
    Constraint: <NSLayoutConstraint:0x7ae4f990 V:[UILabel:0x7ae4ac30'label1']-(20)-[UILabel:0x7ae4f610'label2']>
    Container hierarchy: 
<UILabel: 0x7ae4f610; frame = (0 0; 0 0); text = 'label2'; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x7ae4f720>>
    View not found in container hierarchy: <UILabel: 0x7ae4ac30; frame = (0 0; 0 0); text = 'label1'; userInteractionEnabled = NO; layer = <_UILabelLayer: 0x7ae4c3e0>>
    That view's superview: <UIView: 0x7ae4af30; frame = (0 0; 320 480); autoresize = W+H; layer = <CALayer: 0x7ae4a610>>

Plz help. struck for hours Gone through this but no help

Community
  • 1
  • 1
Rajesh
  • 10,318
  • 16
  • 44
  • 64

1 Answers1

2

Replace this

[labe2 addConstraint:yConstraint];

With

[self.view addConstraint:yConstraint];
Irfan
  • 5,070
  • 1
  • 28
  • 32