Home How do I animate constraint changes?

# How do I animate constraint changes?

user11897
1#
user11897 Published in September 19, 2018, 11:01 am

I'm updating an old app with an AdBannerView and when there is no ad, it slides off screen. When there is an ad it slides on screen. Basic stuff.

Old style, I set the frame in an animation block. New style, I have a IBOutlet to the constraint which determines the Y position, in this case it's distance from the bottom of the superview, and modify the constant.

- (void)moveBannerOffScreen {
[UIView animateWithDuration:5
animations:^{
}];
bannerIsVisible = FALSE;
}

- (void)moveBannerOnScreen {
[UIView animateWithDuration:5
animations:^{
}];
bannerIsVisible = TRUE;
}


And the banner moves, exactly as expected, but no animation.

UPDATE: I re-watched WWDC12 video "Best Practices for Mastering Auto Layout" which covers animation. It discusses how to update constraints using CoreAnimation.

I've tried with the following code, but get the exact same results.

- (void)moveBannerOffScreen {
[UIView animateWithDuration:2
animations:^{
[self.view setNeedsLayout];
}];
bannerIsVisible = FALSE;
}

- (void)moveBannerOnScreen {
[UIView animateWithDuration:2
animations:^{
[self.view setNeedsLayout];
}];
bannerIsVisible = TRUE;
}


On a side note, I have checked numerous times and this is being executed on the main thread.

• I've never seen so many votes offered for a question and answer on a typo on SO before – abbood Sep 30 '13 at 22:25
• If there is a typo in the answer, you should edit the answer. That's why they're editable. – i_am_jorf Oct 6 '14 at 21:13
• @jeffamaphone - It would be more useful if you pointed out the typo so I knew where the mistake was. You could edit the answer yourself and fixed the typo saving everyone else our diatribe. I did just edit it to remove the constant from the animation block, if that's what you were referring to. – DBD Oct 7 '14 at 14:23
• I don't know what the typo is. I was responding to comments above. – i_am_jorf Oct 7 '14 at 16:21
• Then the typo is the question. Stupidly I was typing "setNeedsLayout" instead of "layoutIfNeeded". It's shown clearly in my question when I cut and paste my code with the error and the screenshots with the correct command. Yet couldn't seem to notice it til someone pointed it out. – DBD Oct 7 '14 at 19:39