动画约束时,子视图不能正确调整大小

Subviews do not resize properly when animating constraints

我需要更改视图控制器视图的子视图(称为 playerView)的大小和位置。我已经为需要更改的约束创建了属性。但是当我为变化设置动画时,只有 playerView 是动画的,但它的所有子视图都不是:它们的大小会立即改变。

正文如下:

self.playerViewTop.constant = screenHeight - MinizedPlayerTopOffset;
self.playerViewLeading.constant = MinimizedPlayerOffset;
self.playerViewWidth.constant = MinimizedPlayerWidth;
self.playerViewHeight.constant = MinimizedPlayerHeight;

[UIView animateWithDuration: 2.0 animations: ^
{
    [self.view layoutSubviews];
}];

我不明白为什么会出现这种情况,因为我使用了 autoLayout。请帮忙。

[self.view layoutIfNeeded];
[self.subView setNeedsUpdateConstraints];
[self.subView updateConstraintsIfNeeded];
[UIView animateWithDuration:1.0f delay:0.0f 
options:UIViewAnimationOptionLayoutSubviews animations:^{
[self.view layoutIfNeeded];
} completion:nil];

您需要在动画块中调用 layoutIfNeeded()不是 layoutSubviews()

(实际上你永远不应该自己调用 layoutSubviews()。这是系统在布局过程中调用的方法,仅用于此目的。)