您的位置:首页 > 大数据 > 人工智能

The view hierarchy is not prepared for the constraint

2016-03-22 09:20 459 查看
The view hierarchy is not prepared for the constraint: <NSLayoutConstraint:0x79e7f3f0 UICollectionView:0x7b97a800.top == ScrollView:0x79e935e0.top>
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.


加了某约束之后,(代码加的约束)程序崩溃,日志信息如上所示。

仔细看了下自己的约束代码:

NSLayoutConstraint *top_C    = [NSLayoutConstraint constraintWithItem:self.collectionView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeTop multiplier:1.0 constant:0];
NSLayoutConstraint *left_C   = [NSLayoutConstraint constraintWithItem:self.collectionView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0];
NSLayoutConstraint *right_C  = [NSLayoutConstraint constraintWithItem:self.collectionView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeRight multiplier:1.0 constant:0];
NSLayoutConstraint *height_C = [NSLayoutConstraint constraintWithItem:self.collectionView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeHeight multiplier:1.0 constant:0];


    [self.collectionView
addConstraints:@[top_C,right_C,left_C,height_C]];

看到了问题所在,标红处(即上一句代码中的self.collectionView)应该是item1的父视图而不是他本身。所以说我把它改成self后问题得以解决。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  约束 代码 崩溃