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


以上代码运行结果:


[LayoutConstraints] The view hierarchy is not prepared for the constraint: <NSLayoutConstraint:0x6100000859b0 UIView:0x7f81c8d0ac50.width == UIView:0x7f81c8e08240.width   (inactive)>
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