ViewDidLoad中使用NSLayoutConstraint产生的问题解决
来源:互联网 发布:红心大战 知乎 编辑:程序博客网 时间:2024/06/08 05:15
Demo Code:
UIView *v = [[UIView alloc] initWithFrame:CGRectZero];v.backgroundColor = [UIColor redColor];
v.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:self.v];
NSLayoutConstraint *widthConstraint = [NSLayoutConstraint constraintWithItem:v attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeWidth multiplier:1 constant:0];
NSLayoutConstraint *heightConstraint= [NSLayoutConstraint constraintWithItem:v attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeHeight multiplier:1 constant:0];
[v addConstraints:@[widthConstraint , heightConstraint]];
以上代码运行结果:
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.
[LayoutConstraints] View hierarchy unprepared for constraint.
Constraint: <NSLayoutConstraint:0x6100000859b0 UIView:0x7f81c8d0ac50.width == UIView:0x7f81c8e08240.width (active)>
Container hierarchy:
<UIView: 0x7f81c8d0ac50; frame = (0 0; 0 0); layer = <CALayer: 0x610000038b40>>
View not found in container hierarchy: <UIView: 0x7f81c8e08240; frame = (0 0; 414 736); autoresize = W+H; layer = <CALayer: 0x600000037c80>>
That view's superview: NO SUPERVIEW
NSLayoutConstraint *widthConstraint = [NSLayoutConstraint constraintWithItem:self.view attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:v attribute:NSLayoutAttributeWidth multiplier:1 constant:0];
NSLayoutConstraint *heightConstraint= [NSLayoutConstraint constraintWithItem:self.view attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:v attribute:NSLayoutAttributeHeight multiplier:1 constant:0];
[self.view addConstraints:@[widthConstraint , heightConstraint]];
原理是:
在viewDidLoad方法中,view的层级并没有装载完成,这个方法只是将所有UIView的实例加载到内存中,而没有完成层级的组装,这个时候,针对自定义的UIView子类的实例使用LayoutConstraint,参照对象是self.view, 就会产生Crash。
但是,系统在self.view被显示出来之前,会完成self.view的装载,那么,self.view就是固定的,我们可以依据NSLayoutConstraint类提供的公式view1.attr1 <relation> multiplier × view2.attr2 + c进行反推。也就是解决办法中的用法为什么可以解决问题的原因.
关于公式,请参照:iOS 布局汇总
0 0
- ViewDidLoad中使用NSLayoutConstraint产生的问题解决
- IOS中NSLayoutConstraint的使用
- swift中NSLayoutConstraint布局的使用
- iOS --- 在代码中使用NSLayoutConstraint添加AutoLayout的约束条件
- NSLayoutConstraint使用
- NSLayoutConstraint使用
- NSLayoutConstraint 修改xib中view的高度
- .Net中关于Random类产生的随机数相同问题解决
- XML文件中nbsp的产生和问题解决
- awakeFromNib和 viewDidLoad的使用时机
- loadView、viewDidLoad的使用及区别
- Autolayout 中 NSLayoutConstraint
- Objectiv-C UIKit基础 NSLayoutConstraint的使用(VFL实现)
- NSLayoutConstraint 使用代码实现约束的添加和删除
- Unity中使用LitJson产生的bug
- Android中ListView放入PopupWindow产生问题解决
- NSLayoutConstraint
- NSLayoutConstraint
- 每天一个linux命令:mv命令
- 理解关键的渲染路径
- About Restful arch. & api.
- 数的划分
- 学术会议 Rebuttal 模板
- ViewDidLoad中使用NSLayoutConstraint产生的问题解决
- WebRTC音频
- I - Misha and Changing Handles CodeForces - 501B
- 八种消除沟通上的不良习惯地的方法
- hdu1045【dfs】
- redis面试题汇总
- java中类中创建本类实例的疑问
- 稳定婚姻问题(The Stable Marriage Problem)
- 获取Spring中bean对象或在Spring初始化完成之后使用Spring中bean对象