Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto Layout constraint change does not animate

I was learning the auto layout with animations from the tutorial

http://weblog.invasivecode.com/post/42362079291/auto-layout-and-core-animation-auto-layout-was

and things were working perfect.

When I tried to use this concept in my application, trying to animate a settings screen(a UIView) from bottom to top,it works great when the settings screen is just an empty UIView,

But in case I add a UILabel as a subview to this settings screen, the animation just vanishes. On removing this UILabel form the settings screen, the animation is visible.

Here are the outlets that I have connected

__weak IBOutlet UIView *settingsView;
__weak IBOutlet NSLayoutConstraint *settingsBottomConstraint;
__weak IBOutlet NSLayoutConstraint *settingsViewHeightConstraint;

View did load setup method(setupViews)

-(void)setupViews
{
    settingsBottomConstraint.constant = - settingsViewHeightConstraint.constant;
    [settingsView setNeedsUpdateConstraints];
    [settingsView layoutIfNeeded];
    isSettingsHidden = YES;
}

Hide/Unhide Method

- (IBAction)showSettingsScreen:(id)sender {

    if (isSettingsHidden) {

        settingsBottomConstraint.constant = 0;
        [settingsView setNeedsUpdateConstraints];
        [UIView animateWithDuration:.3 animations:^{
            [settingsView layoutIfNeeded];
        }];
    }
    else{

        settingsBottomConstraint.constant = - settingsViewHeightConstraint.constant;
        [settingsView setNeedsUpdateConstraints];
        [UIView animateWithDuration:0.3 animations:^{
            [settingsView layoutIfNeeded];
        }];

    }
    isSettingsHidden = !isSettingsHidden;
}

My issue seems similar to the Issue with UIView Auto Layout Animation

like image 291
BangOperator Avatar asked Apr 12 '14 09:04

BangOperator


People also ask

What is auto layout?

Auto layout is a property you can add to frames and components. It lets you create designs that grow to fill or shrink to fit, and reflow as their contents change. This is great when you need to add new layers, accommodate longer text strings, or maintain alignment as your designs evolve.


1 Answers

I found the answer.

Instead of,

[settingsView layoutIfNeeded];

this line made it worked like charm,

[self.view layoutIfNeeded];

I suppose we need to perform layoutIfNeeded method on the parent view not just the view we are trying to animate.

UPDATE: As pointed out in a comment by codyko, this is required for iOS 7, iOS 10. For iOS 8 this issue does not exists.

like image 81
BangOperator Avatar answered Oct 03 '22 15:10

BangOperator