我已使用UIView
在UIScrollview
内添加了UIView
和IB
。这是我的视图层次结构。
现在我以编程方式将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.
请帮帮我。 感谢