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后问题得以解决。
相关文章推荐
- android 代码实现控件之间的间距
- [Android]在代码里运行另一个程序的方法
- 肯特·贝克:改变人生的代码整理魔法
- 网页恶意代码的预防
- 高手写的Tracer-Flash代码调试类代码下载
- CSS代码缩写技巧
- 非主流Q-zOne代码代码搜集第1/2页
- CreateWeb.vbs 代码
- Lua中编译执行代码相关的函数详解
- SQLserver删除某数据库中所有表实现思路
- 更有效率的css代码编写第1/3页
- 代码中到底应不应当写注释?
- SQL语言查询基础:连接查询 联合查询 代码
- 论坛头像随机变换代码
- .NET 常用功能和代码小结
- C#实现压缩HTML代码的方法
- asp编程中常用的javascript辅助代码第1/2页
- 高质量PHP代码的50个实用技巧必备(下)
- C#超实用代码段合集
- Javascript代码在页面加载时的执行顺序介绍