本文介绍了UICollectionView在保持位置上方插入单元格(例如Messages.app)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
默认情况下,收藏夹视图"在插入单元格时保持内容偏移.另一方面,我想在当前显示的单元格上方插入单元格,以便它们在加载较早的消息时像Messages.app一样显示在屏幕顶部上方.有人知道实现这一目标的方法吗?
By default Collection View maintains content offset while inserting cells. On the other hand I'd like to insert cells above the currently displaying ones so that they appear above the screen top edge like Messages.app do when you load earlier messages. Does anyone know the way to achieve it?
推荐答案
这是我使用的技术.我发现其他原因会引起奇怪的副作用,例如屏幕闪烁:
This is the technique I use. I've found others cause strange side effects such as screen flicker:
CGFloat bottomOffset = self.collectionView.contentSize.height - self.collectionView.contentOffset.y;
[CATransaction begin];
[CATransaction setDisableActions:YES];
[self.collectionView performBatchUpdates:^{
[self.collectionView insertItemsAtIndexPaths:indexPaths];
} completion:^(BOOL finished) {
self.collectionView.contentOffset = CGPointMake(0, self.collectionView.contentSize.height - bottomOffset);
}];
[CATransaction commit];
这篇关于UICollectionView在保持位置上方插入单元格(例如Messages.app)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!