在 containerview 上使用 layoutIfNeeded 时闪烁

Flicker while using layoutIfNeeded on containerview

我在故事板中有一个包含两个容器视图的控制器。我将两个容器之间的垂直距离设置为零。

我想要的是在 运行 时间更改其中一个容器的高度限制。

这是更改约束的代码:

[UIView animateWithDuration:kAnimationDuration animations:^{
    self.offeringContainerHeightConstraint.constant = [SJDataManager shared].offeringItems.count * kOfferingCellHeight + kOfferingHeaderHeight;
    [self.view layoutIfNeeded];
}];

问题是,另一个容器视图在前面移动,因此两个容器之间存在空白区域,直到动画完成。

我希望两个容器视图应该同步更改它们的约束值,以便可以消除这种闪烁。

我终于找到了解决办法。实际上这是 expectedHeightForRowAtIndexPath 和 heightForRowAtIndexPath 之间的差异。由于这两种方法返回的高度不同,这就是我出现闪烁的原因。