UILabel is not animated inside UIView, automatic automata are used

I have a very simple application. It contains a red view and a green label inside it. There is also a button that starts the animation. I am updating the red view by changing its width constraint constant value.

[self.view layoutIfNeeded];
[UIView animateWithDuration:0.4 animations:^{
    self.widthConstraint.constant = width;
    [self.view layoutIfNeeded];
}];

      

The red view animates as expected, but the green label inside changes its width instantly. I've played around with various animation flags, tried some advice from SO but didn't get them working.

Please find sources here .

TIA

+3


source to share


2 answers


I seem to have found the root of the problem. Limitations are not a problem, they UILabel

are.

If you replace UILabel

in my project with a simple one UIView

, it will be smooth as expected. It's a matter of animating content UILabel

. You can achieve the right animation set label.contentMode = UIViewContentModeCenter

, but it looks ugly.



The best way to solve this problem is not to try to resize UILabel

while animating. Try to think of a different behavior. In my case, I change the UILabel

alpha to 0 and the x position is larger than the width of the container.

Learn more about UILabel

animation here .

+7


source


I looked at your project. I don’t have much experience with setting constraints from the editor, but I would highly recommend that you try it in code in case of problems. Personally, I would animate the width constraint of this red view and set constraints on the label margins with a visual format, it would be something like this: @"H:|[label]|"

and add this constraint to the red view. This should fix the problem.



0


source







All Articles