Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Autolayout crash when applying transform: Assertion failure in -[layoutSublayersOfLayer:]

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

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

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.

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?

like image 237
Antonio E. Avatar asked Jul 03 '14 09:07

Antonio E.


2 Answers

I had this issue in iOS7 (but not iOS8) from putting constraint adjustments in viewWillLayoutSubviews. Moving constraint adjustment to viewWillAppear fixed it for me.

like image 183
Sean Avatar answered Nov 19 '22 17:11

Sean


In iOS7 you need to do this on viewDidAppear. It will also work for iOS8. If you dont want to see the delayed element change on screen, hide your super view on viewDidLoad and unhide it on viewDidAppear, right after you apply your changes.

like image 25
Marcelo Ribeiro Avatar answered Nov 19 '22 18:11

Marcelo Ribeiro