ViewDidLoad中使用NSLayoutConstraint产生的问题解决


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:
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:
Container hierarchy: 
>
View not found in container hierarchy: >

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 multiplier × view2.attr2 + c进行反推。也就是解决办法中的用法为什么可以解决问题的原因.

关于公式,请参照:iOS 布局汇总



你可能感兴趣的:(Apple)