iosswiftuicollectionviewmessagekit

Chat View built with MessageKit never scrolls to last message with a picture


I'm building a chat view for my app with MessageKit. Image messages are something that this chat view will support. I'm currently having an issue where if the last message is a picture message, the message won't be properly displayed:

I'm loading all my messages in viewDidLoad, and then updating the MessagesCollectionView in viewWillAppear:

 override func viewWillAppear(_ animated: Bool) {
        messagesCollectionView.reloadData()
        messagesCollectionView.scrollToLastItem()
    }

How do I get image messages to be properly displayed?

enter image description here


Solution

  • It appears that the issue was that these updates were not being called on the right thread; modifying the code to the below solved the issue:

    override func viewWillAppear(_ animated: Bool) {
        DispatchQueue.main.async {
            self.messagesCollectionView.reloadData()
            self.messagesCollectionView.scrollToLastItem()
        }
    }