UICollectionView insert cells above maintaining position (like Messages.app)

后端 未结 20 2064
渐次进展
渐次进展 2020-12-02 07:23

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 appea

20条回答
  •  天命终不由人
    2020-12-02 08:23

    CGPoint currentOffset = _collectionView.contentOffset;
    CGSize contentSizeBeforeInsert = [_collectionView.collectionViewLayout collectionViewContentSize];
    
    [_collectionView reloadData];
    
    CGSize contentSizeAfterInsert = [_collectionView.collectionViewLayout collectionViewContentSize];
    
    CGFloat deltaHeight = contentSizeAfterInsert.height - contentSizeBeforeInsert.height;
    currentOffset.y += MAX(deltaHeight, 0);
    
    _collectionView.contentOffset = currentOffset;
    

提交回复
热议问题