Auto Layout constraint change does not animate

爷,独闯天下 提交于 2019-12-18 02:48:14

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/23028701/auto-layout-constraint-change-does-not-animate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!