具有自调整大小的单元格的UICollectionView在插入/移除动画期间调整单元格大小

Sen*_*ior 26 ios autolayout uicollectionview ios8

我有一个带有UICollectionViewFlowLayout的简单UICollectionView,它使用autolayout来允许自定义单元格.在静态布局中,事情很有效,但是当我插入或删除单元格时,单元格会调整为估计的大小:

[self.collectionView performBatchUpdates:^{
    [self.collectionView insertItemsAtIndexPaths:@[indexPath]];
} completion:nil];
Run Code Online (Sandbox Code Playgroud)

除了设置估计的大小,像这样:

layout.estimatedItemSize = CGSizeMake(88.0, 88.0);
Run Code Online (Sandbox Code Playgroud)

我在集合视图中没有做任何奇特的事情.一切都是使用故事板设置的.

我怎样才能避免这种大小调整?我只针对iOS 8.0及以上,因此应该支持它.

问题的例子

Jož*_* Ws 0

一个简单的解决方案是实现 UICollectionViewDelegateFlowLayout 方法

 func collectionView(_ collectionView: UICollectionView, layoutcollectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
      // calculate string size here
}
Run Code Online (Sandbox Code Playgroud)

插入或删除单元格后,请确保布局无效,以便再次调用委托方法

collectionView.performBatchUpdates({ 
    // insert/delete items here
}) { (success: Bool) in
    collectionView.collectionViewLayout.invalidateLayout()
}
Run Code Online (Sandbox Code Playgroud)