我注意到在 iOS 10 Beta 5(即将试用 Beta 6)中,AutoLayout 约束动画的行为有点不同。
例如,这种方法与以前的 iOS 版本不同:
[view addConstraints:@[constraints...]];
[view setNeedsUpdateConstraints];
[view layoutIfNeeded];
[UIView animateWithDuration:...
{
/* adjust constraint here... */
[view layoutIfNeeded]; // Only the adjusted constraints since previous layoutIfNeeded() call should animate change with duration.
} completion:{ ... }];
...在我的测试中,最初添加的约束是
addConstraints()
将还有在 iOS 10 中使用 UIView 制作动画 animateWithDuration()
阻止......到目前为止,这导致了一些时髦/不受欢迎的行为。例如,设置数组中的左/右约束(但 block 中的垂直约束)使整个 View 沿对角线在屏幕上动画用这种方法......这是完全不正确的。
有谁知道如何为 iOS 9(及更低版本)以及 10 正确执行此操作?
最佳答案
尝试调用layoutIfNeeded
在 View 的 super View 上,而不是 View 本身。
--
我有一个类似的问题,我的 View 应该在 0 高度从顶部动画,向下到 > 0 高度(即 (0, 0, 320, 0) 到 (0, 0, 320, 44))。
在 iOS 10 中使用 Xcode 8,动画行为有很大不同。 View 不是从顶部向下动画,而是从目标帧的垂直中心向上和向下动画。
我解决了这个问题,而不是调用 layoutIfNeeded
在 View 本身上,在 View 的父 View 上调用它。不知何故,行为恢复了。
希望能帮助到你!
@Ramiro 的有用评论:
According to the documentation,
layoutIfNeeded
lays out the subviews (but doesn't contemplate the current view itself). So, in order to update the current view lay out, you need to calllayoutIfNeeded
from the super view.
关于animation - 处理 iOS 10 中的 AutoLayout 约束动画差异?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38965727/