Autolayout在应用转换时崩溃: – 中的声明失败

我有一个自定义的视图,在右下angular显示一个UILabel。 该视图是从initWithCoder:initWithFrame:调用的方法中设置的initWithFrame:像这样:

 MCLabel* likesLabel = [[MCLabel alloc] init]; likesLabel.mc_textPadding = UIEdgeInsetsMake(0, 10, 0, 10); likesLabel.font = [UIFont fontWithName:@"FontAwesome" size:12.f]; [likesLabel setText:@"test"]; likesLabel.numberOfLines = 2; likesLabel.backgroundColor = [UIColor colorWithWhite:1 alpha:.8]; likesLabel.textColor = UIColor.blackColor; likesLabel.translatesAutoresizingMaskIntoConstraints = NO; likesLabel.textAlignment = NSTextAlignmentCenter; likesLabel.mc_verticalTextAlignment = MCVerticalTextAlignmentTop; [self addSubview:likesLabel]; self.likesLabel = likesLabel; NSLayoutConstraint* widthConstraint = [NSLayoutConstraint constraintWithItem:likesLabel attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeWidth multiplier:1 constant:1]; NSLayoutConstraint* heightConstraint = [NSLayoutConstraint constraintWithItem:likesLabel attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:likesLabel attribute:NSLayoutAttributeWidth multiplier:2/5.f constant:1]; NSLayoutConstraint* horizontalPosition = [NSLayoutConstraint constraintWithItem:likesLabel attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeRight multiplier:1 constant:1]; NSLayoutConstraint* verticalPosition = [NSLayoutConstraint constraintWithItem:likesLabel attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeBottom multiplier:1 constant:1]; [likesLabel addConstraints:@[heightConstraint]]; [self addConstraints:@[widthConstraint, horizontalPosition, verticalPosition]]; 

现在,如果我把这样的东西都留下来了,我没有任何问题,但是只要对这个标签应用一个转换(这是UILabel的一个子类,只是简单地添加垂直alignment和边缘插入),应用程序就会崩溃控制台中的错误:

 *** Assertion failure in -[MCView layoutSublayersOfLayer:], /SourceCache/UIKit/UIKit-2935.138/UIView.m:8794 Auto Layout still required after executing -layoutSubviews 

断言暗示可能是子类在重写方法时没有调用[super layoutSubviews] ,但我做了。

由于很明显,这里的问题是自动布局设置,恐怕我忽略了一些东西,也许布局是模棱两可的,因此崩溃。

还有一点需要注意的是:如果我在- (void)didMoveToSuperview方法中移动变换,那么相同的代码不会在iOS 8上崩溃。

任何人可以帮助这里?

我在iOS7(但不是iOS8)中将此问题从viewWillLayoutSubviews进行约束调整。 移动约束调整viewWillAppear为我修复它。

在iOS7中,您需要在viewDidAppear上执行此操作。 它也将适用于iOS8。 如果您不想在屏幕上看到延迟的元素更改,请在viewDidLoad上隐藏超级视图,并在应用更改后将其隐藏在viewDidAppear上。