Animate autoLayout 约束不适用于 iOS 7

Animate autoLayout constraints doesn't work on iOS 7

我有一个 UIButton,它只有 4 个与其高度、宽度、顶部、左侧相关的约束。 当用户点击按钮时,我通过添加一个像这样的常量值来更新它的顶部约束

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

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

当我 运行 我的应用程序在 iOS 8 时它的工作就像一个魅力,但在 iOS 7 约束动画不起作用,按钮基本上从点 a 移动指向 b 没有任何动画

PS : 我试图在开始动画之前放置更新约束指令,但它也没有用

我该如何解决这个问题?我做错了什么?谢谢你的帮助

您按错误的顺序执行操作,并且忘记通知布局系统需要布局。像这样:

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

在我的测试中,在 iOS 7、iOS 8 和 iOS 9 中进行动画处理。如果它对你不起作用,那么你正在做其他事情你还没有透露。

Swift版本在这里。

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