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

后端 未结 20 2105
渐次进展
渐次进展 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:11

    Swift 3 version code: based on James Martin answer

        let amount = 1 // change this to the amount of items to add
        let section = 0 // change this to your needs, too
        let contentHeight = self.collectionView.contentSize.height
        let offsetY = self.collectionView.contentOffset.y
        let bottomOffset = contentHeight - offsetY
    
        CATransaction.begin()
        CATransaction.setDisableActions(true)
    
        self.collectionView.performBatchUpdates({
          var indexPaths = [NSIndexPath]()
          for i in 0.. 0 {
            self.collectionView.insertItems(at: indexPaths as [IndexPath])
          }
        }, completion: {
           finished in
           print("completed loading of new stuff, animating")
           self.collectionView.contentOffset = CGPoint(x: 0, y: self.collectionView.contentSize.height - bottomOffset)
           CATransaction.commit()
        })
    

提交回复
热议问题