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 the screen. Basic stuff.
O
Generally, you just need to update constraints and call layoutIfNeeded
inside the animation block. This can be either changing the .constant
property of an NSLayoutConstraint
, adding remove constraints (iOS 7), or changing the .active
property of constraints (iOS 8 & 9).
[UIView animateWithDuration:0.3 animations:^{
// Move to right
self.leadingConstraint.active = false;
self.trailingConstraint.active = true;
// Move to bottom
self.topConstraint.active = false;
self.bottomConstraint.active = true;
// Make the animation happen
[self.view setNeedsLayout];
[self.view layoutIfNeeded];
}];
There are some questions about whether the constraint should be changed before the animation block, or inside it (see previous answers).
The following is a Twitter conversation between Martin Pilkington who teaches iOS, and Ken Ferry who wrote Auto Layout. Ken explains that though changing constants outside of the animation block may currently work, it's not safe and they should really be change inside the animation block. https://twitter.com/kongtomorrow/status/440627401018466305
Here's a simple project showing how a view can be animated. It's using Objective C and animates the view by changing the .active
property of several constraints.
https://github.com/shepting/SampleAutoLayoutAnimation