The view hierarchy is not prepared for the constraint in objective c

275 Views Asked by At

I have added UIViewand UIScrollviewinside that UIViewusing IB.This is my view hierarchy.

Now I am adding UIViews into my UIScrollViewprogrammatically and im adding constraints programmatically like this.

-(void)loadLeavesBlocks
{
for(int x=0;x<[arrayToday count];x++)
{
    vwBlock=[[UIView alloc] initWithFrame:CGRectMake(blockX, blockY, blockWidth, 250.0)];
    vwBlock.translatesAutoresizingMaskIntoConstraints = NO;
    [vwBlock setBackgroundColor:[UIColor whiteColor]];
    [_mainScroll addSubview:vwBlock];

    NSLayoutConstraint *top = [NSLayoutConstraint
                                   constraintWithItem:vwBlock
                                   attribute:NSLayoutAttributeTop
                                   relatedBy:NSLayoutRelationEqual
                                   toItem:_segmentedControl
                                   attribute:NSLayoutAttributeBottom
                                   multiplier:1.0f
                                   constant:blockY];

    NSLayoutConstraint *leading = [NSLayoutConstraint
                                   constraintWithItem:vwBlock
                                   attribute:NSLayoutAttributeLeading
                                   relatedBy:NSLayoutRelationEqual
                                   toItem:_mainScroll
                                   attribute:NSLayoutAttributeLeading
                                   multiplier:1.0f
                                   constant:blockX];

    NSLayoutConstraint *trailling = [NSLayoutConstraint
                                   constraintWithItem:vwBlock
                                   attribute:NSLayoutAttributeLeading
                                   relatedBy:NSLayoutRelationEqual
                                   toItem:_mainScroll
                                   attribute:NSLayoutAttributeLeading
                                   multiplier:1.0f
                                   constant:10.f];

    //Height to be fixed for SubView same as AdHeight
    NSLayoutConstraint *height = [NSLayoutConstraint
                                  constraintWithItem:vwBlock
                                  attribute:NSLayoutAttributeHeight
                                  relatedBy:NSLayoutRelationEqual
                                  toItem:nil
                                  attribute:NSLayoutAttributeNotAnAttribute
                                  multiplier:0
                                  constant:250.f];

    [_mainScroll addConstraint:top];
    [_mainScroll addConstraint:leading];
    [_mainScroll addConstraint:trailling];
    [vwBlock addConstraint:height];





    blockY=blockY+260;

}

[hud removeFromSuperview];

}

But I am getting this error.

The view hierarchy is not prepared for the constraint: <NSLayoutConstraint:0x15eddb560 V:[UISegmentedControl:0x15eda4bc0]-(0)-[UIView:0x15edc3a50]>
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.

Please help me. Thanks

enter image description here

0

There are 0 best solutions below