且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在通过动画调整UIView大小时,约束不起作用

更新时间:2023-09-08 19:46:34

当尝试使用自动版式制作动画时,您应该直接更改视图的帧。

When trying to animate with Auto Layout, you should not change view's frames directly. Instead, you should animate changes in constraints.

要从故事板访问约束,您可以简单地为他们创建IBOutlets

[containerView layoutIfNeeded]; // complete pending layout operations (optional)
[UIView animateWithDuration:1.0 animations:^{

    // Update constraints
    self.viewConstraint1.constant = NEW_VALUE;
    self.viewConstraint2.constant = NEW_VALUE;

    // Animate the changes
    [containerView layoutIfNeeded]; 
}];

注意,你通常想要调用layoutIfNeeded在超级视图

我建议您阅读Apple的官方指南

I recommend reading more on Auto Layout in Apple's official guide.