应用自动布局崩溃时变换:断言失败 - [layoutSublayersOfLayer:] [英] Autolayout crash when applying transform: Assertion failure in -[layoutSublayersOfLayer:]
问题描述
我在右下角显示一个UILabel自定义视图。视图是一个距离 initWith codeR调用的方法设置:
和 initWithFrame:方法
是这样的:
I have a custom view that display a UILabel in the bottom right corner. The view is setup in a method called from both initWithCoder:
and initWithFrame:
like this:
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的子类,只需添加垂直对齐和边缘插图如果该事项)的应用程序的错误崩溃控制台:
Now if I leave everything like this I do not have any kind of problem but, as soon as I apply a transform to this label (that is a subclass of UILabel that simply add vertical alignment and edge insets if that matters) the app crashes with the error in console:
*** Assertion failure in -[MCView layoutSublayersOfLayer:], /SourceCache/UIKit/UIKit-2935.138/UIView.m:8794
Auto Layout still required after executing -layoutSubviews
断言暗示可能是子类没有叫 [超级layoutSubviews]覆盖的方法,但我没有当
。
因为很明显,这里的问题是自动版式设置我怕我的东西,可俯瞰也许布局暧昧因此崩溃。
Since is clear that the problem here is the autolayout setup I'm afraid that I'm overlooking at something and maybe the layout is ambiguous hence the crash.
还要说明一点:在同一code不iOS上8崩溃,如果我谨在变换 - (无效)didMoveToSuperview
法
One more note: the same code does not crash on iOS 8 if I move the transform in the - (void)didMoveToSuperview
method.
任何人都可以在这里帮助?
Anyone that can helps here?
推荐答案
我从把约束的调整 viewWillLayoutSubviews
曾在iOS7(但不是iOS8上)这个问题。移动约束调整 viewWillAppear中
固定对我来说。
I had this issue in iOS7 (but not iOS8) from putting constraint adjustments in viewWillLayoutSubviews
. Moving constraint adjustment to viewWillAppear
fixed it for me.
这篇关于应用自动布局崩溃时变换:断言失败 - [layoutSublayersOfLayer:]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!