Search code examples
iosswiftuicollectionviewuiscrollviewmessagekit

Show the bottom of collectionView on first load without scrolling and lazy loading


I have developed a Chat App, where I used MessageKit for my iOS app. My issues is when I open any Chat screen, messageCollectionView starts from top ,showing old chats. I need to start the collectionView from bottom as soon as I open any chat screen. I have tried :

messagesCollectionView.scrollToBottom(animated: true)

but there is a performance impact (in case there and more than hundreds/thousands of messages).

Is there any way if inverting the collection view, or starting to add the cells from bottom and then top.


Solution

  • The above suggestions in comments were more robust and correct implementation.


    alternatively you can try this:

    //After reloading
    DispatchQueue.main.async {
        self.messagesCollectionView.scrollToItem(at: IndexPath(row: 0, section: self.messages.count - 1), at: .top, animated: false)
       }