0

As I see, AutoLayouts sets before rotate animation, instantly. And for animation of transition (scale, for example) I must call my custom method from -(void)willRotateToInterfaceOrientation. So, is there something to animate autolayout (constraints) when device rotates?

UPD: I check it up, and it realy works, but only with views. Is there something to work with labels?

// UIView
UIView *yellowView = [[UIView alloc] init];
yellowView.backgroundColor = [UIColor yellowColor];
[self.view addSubview:yellowView];
yellowView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:yellowView
                                                      attribute:NSLayoutAttributeLeading
                                                      relatedBy:NSLayoutRelationEqual
                                                         toItem:self.view
                                                      attribute:NSLayoutAttributeLeading
                                                     multiplier:1
                                                       constant:40]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:yellowView
                                                      attribute:NSLayoutAttributeTrailing
                                                      relatedBy:NSLayoutRelationEqual
                                                         toItem:self.view
                                                      attribute:NSLayoutAttributeTrailing
                                                     multiplier:1
                                                       constant:-40]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:yellowView
                                                      attribute:NSLayoutAttributeTop
                                                      relatedBy:NSLayoutRelationEqual
                                                         toItem:self.view
                                                      attribute:NSLayoutAttributeTop
                                                     multiplier:1
                                                       constant:40]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:yellowView
                                                      attribute:NSLayoutAttributeBottom
                                                      relatedBy:NSLayoutRelationEqual
                                                         toItem:self.view
                                                      attribute:NSLayoutAttributeBottom
                                                     multiplier:1
                                                       constant:-40]];
[yellowView layoutIfNeeded];

// UILabel
UILabel *greenLabel = [[UILabel alloc] initWithFrame:self.view.frame];
greenLabel.backgroundColor = [UIColor greenColor];
[self.view addSubview:greenLabel];
greenLabel.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:greenLabel
                                                      attribute:NSLayoutAttributeLeading
                                                      relatedBy:NSLayoutRelationEqual
                                                         toItem:self.view
                                                      attribute:NSLayoutAttributeLeading
                                                     multiplier:1
                                                       constant:40]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:greenLabel
                                                      attribute:NSLayoutAttributeTrailing
                                                      relatedBy:NSLayoutRelationEqual
                                                         toItem:self.view
                                                      attribute:NSLayoutAttributeTrailing
                                                     multiplier:1
                                                       constant:-40]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:greenLabel
                                                      attribute:NSLayoutAttributeTop
                                                      relatedBy:NSLayoutRelationEqual
                                                         toItem:self.view
                                                      attribute:NSLayoutAttributeTop
                                                     multiplier:1
                                                       constant:40]];
[self.view addConstraint:[NSLayoutConstraint constraintWithItem:greenLabel
                                                      attribute:NSLayoutAttributeBottom
                                                      relatedBy:NSLayoutRelationEqual
                                                         toItem:self.view
                                                      attribute:NSLayoutAttributeBottom
                                                     multiplier:1
                                                       constant:-40]];
[greenLabel layoutIfNeeded];

UPD2: I test animation and it's look like UILabels problem. Labels downsizing not animates.

UILabel *pupureLabel = [[UILabel alloc] init];
pupureLabel.backgroundColor = [UIColor pupureColor];
[self.view addSubview:pupureLabel];
pupureLabel.frame = CGRectMake(0, 0, 320, 568);
[UIView animateWithDuration:1
    animations:^{
        pupureLabel.frame = CGRectMake(0, 0, 100, 60);
    }];
ExeiL
  • 15
  • 5
  • Your question is a big vague. Do you mean the animation is not smooth right now? Then you better give some code on how have setup things. Do you mean you change the constraints prior to the rotation? Or do you mean, you want to see them for debug reasons? – fishinear Nov 22 '14 at 22:13
  • You don't understand - there is **no** animation right now. Constraints take effect **instantly!** Turn on "slo-mo animation" in simulator and begin rotate device, so you will see. When device begin rotates, constraints changes immediately - without animation. You may see animation of rotation, but **not** autolayout animation. – ExeiL Nov 23 '14 at 09:53
  • Animation of constraints works fine for me. That's why I ask what you are doing. Do you setup the constraints in Interface Builder, or in the source code? Do you change them right before the rotation? Do you do something else with them? Show some code. What does your custom method do, for example? – fishinear Nov 23 '14 at 10:40
  • Hmm... I check it up, and it realy works, but only with views. Is there something to work with labels? – ExeiL Nov 23 '14 at 11:04
  • Wow. You are right. I just tried it out myself, and it does not work for me either. It looks like a definite bug to me; report it to Apple. And it seems to be an older problem as well, see e.g. [this other question](http://stackoverflow.com/questions/17022804/autolayout-and-uilabel-resize-animation-not-working-properly-on-device-rotation) – fishinear Nov 23 '14 at 15:09
  • http://stackoverflow.com/a/27013001/1702413 – TonyMkenu Nov 23 '14 at 19:35

1 Answers1

1

UILabel is a bit of a strange beast regarding AutoLayout constraints. It defines an intrinsicContentSize that is computed from the text that it contains. The intrinsicContentSize acts as an implicit constraint: it fixes the width and height of the label. Therefore, if you also add leading and trailing constraints to the label, then you have conflicting constraints, resulting in the behaviour that you see.

This behaviour is complicated for multi-line labels by the preferredMaxLayoutWidth, determining what its preferred width is.

With all that said, I still think it is still a bug in UILabel that it does not animate its size change.

In the meantime, you probably need to ask yourself why you want the label to resize (instead of centering it in its parent, for example).

fishinear
  • 6,101
  • 3
  • 36
  • 84