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

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

    Inspired by Bryan Pratte's solution I developed subclass of UICollectionViewFlowLayout to get chat behavior without turning collection view upside-down. This layout is written in Swift 3 and absolutely usable with RxSwift and RxDataSources because UI is completely separated from any logic or binding.

    Three things were important for me:

    1. If there is a new message, scroll down to it. It doesn't matter where you are in the list in this moment. Scrolling is realized with setContentOffset instead of scrollToItemAtIndexPath.
    2. If you do "Lazy Loading" with older messages, then the scroll view shouldn't change and stays exactly where it is.
    3. Add exceptions for the beginning. The collection view should behave "normal" till there are more messages than space on the screen.

    My solution: https://gist.github.com/jochenschoellig/04ffb26d38ae305fa81aeb711d043068

提交回复
热议问题