mahieddine
mahieddine

Reputation: 565

Animate autoLayout constraints doesn't work on iOS 7

I have an UIButton that has only 4 constraints related to its height,width, top, left. When the user tap on the button i update its top constraint by adding a constant value like this

[self.view layoutIfNeeded];
[UIView animateWithDuration:0.4 animations:^{
    [self updateMyButtonConstraints];
    [self.view layoutIfNeeded];
}];

-(void)updateMyButtonConstraints {
   myTopButtonConstraint.constant = 10;
}

When i run my app on iOS 8 its works like a charm, but on iOS 7 the constraint animation doesn't work, the button basically moves from point a to point b without any animation

PS : i tried to put the update constraint instruction before starting the animation and its didn't work too

How can i do to fix this ? What i'm doing wrong ? thank you for the help

Upvotes: 0

Views: 873

Answers (2)

Utku
Utku

Reputation: 173

Swift version is here.

self.updateMyButtonConstraints()
self.view.setNeedsLayout()
UIView.animate(withDuration: 0.4) {
    self.view.layoutIfNeeded()
}

Upvotes: 1

matt
matt

Reputation: 535138

You're doing things in the wrong order, and you're forgetting to notify the layout system that layout is needed. Like this:

[self updateMyButtonConstraints];
[self.view setNeedsLayout]; // not "layoutIfNeeded"!
[UIView animateWithDuration:0.4 animations:^{
    [self.view layoutIfNeeded];
}];

In my tests, that animates in iOS 7, iOS 8, and iOS 9. If it doesn't work for you, you're doing something else that you have not revealed.

Upvotes: 4

Related Questions