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

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

    // stop scrolling
    setContentOffset(contentOffset, animated: false)
    
    // calculate the offset and reloadData
    let beforeContentSize = contentSize
    reloadData()
    layoutIfNeeded()
    let afterContentSize = contentSize
    
    // reset the contentOffset after data is updated
    let newOffset = CGPoint(
      x: contentOffset.x + (afterContentSize.width - beforeContentSize.width),
      y: contentOffset.y + (afterContentSize.height - beforeContentSize.height))
    setContentOffset(newOffset, animated: false)
    

提交回复
热议问题