我有一个在右下角显示 UILabel 的自定义视图.视图是在从 initWithCoder:
和 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
断言提示可能子类在覆盖方法时没有调用 [super layoutSubviews]
但我确实调用了.
The assertion hints that probably the subclass didn't call [super layoutSubviews]
when overriding the methods but I did.
由于很明显这里的问题是自动布局设置,我担心我忽略了一些东西,也许布局不明确,因此崩溃.
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.
另一个注意事项:如果我在 - (void)didMoveToSuperview
方法中移动转换,相同的代码不会在 iOS 8 上崩溃.
One more note: the same code does not crash on iOS 8 if I move the transform in the - (void)didMoveToSuperview
method.
有谁可以帮忙吗?
我在 iOS7(但不是 iOS8)中遇到了这个问题,原因是在 viewWillLayoutSubviews
中进行了约束调整.将约束调整移动到 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:] 中的断言失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!