设置自动布局约束动画不';我不能在iOS 7上工作
我有一个UIButton,它只有4个与高度、宽度、顶部和左侧相关的约束。 当用户点击按钮时,我通过添加如下常量值来更新其顶部约束设置自动布局约束动画不';我不能在iOS 7上工作,ios,objective-c,cocoa-touch,autolayout,constraints,Ios,Objective C,Cocoa Touch,Autolayout,Constraints,我有一个UIButton,它只有4个与高度、宽度、顶部和左侧相关的约束。 当用户点击按钮时,我通过添加如下常量值来更新其顶部约束 [self.view layoutIfNeeded]; [UIView animateWithDuration:0.4 animations:^{ [self updateMyButtonConstraints]; [self.view layoutIfNeeded]; }]; -(void)updateMyButtonConstraints {
[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中都是动画。如果它对你不起作用,你在做一些你没有透露的事情。你按错误的顺序做事情,并且忘记通知布局系统需要布局。像这样:
[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()
}
Swift版本在这里
self.updateMyButtonConstraints()
self.view.setNeedsLayout()
UIView.animate(withDuration: 0.4) {
self.view.layoutIfNeeded()
}
尝试在按钮上调用setNeedsLayout,然后在按钮上调用LayoutNeeded,而不是在self上。视图我尝试过,也不起作用:(尝试在按钮上调用setNeedsLayout,然后在按钮上调用LayoutNeeds,而不是在self上。视图我尝试过,也不起作用:(请注意,这假设
self.view
是按钮的超视图。如果不是,则相应地更改代码:您想在此处与之对话的是按钮的超视图。请注意,这假设self.view
是按钮的超视图。如果不是,则相应地更改代码:您想要的是按钮的超视图。)在这里交谈。