Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
427 views
in Technique[技术] by (71.8m points)

ios - 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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...