视图层次结构未针对目标c中的约束进行准备

时间:2016-12-26 20:06:03

标签: ios objective-c constraints ios-autolayout

我已使用UIViewUIScrollview内添加了UIViewIB。这是我的视图层次结构。

现在我以编程方式将UIView添加到我的UIScrollView中,并以编程方式添加约束。

-(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];

}

但我收到了这个错误。

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.

请帮帮我。 感谢

enter image description here

0 个答案:

没有答案