0

After tapping on a conversation, the chat screen will start at the top and then scroll all the way to the bottom. How can i bypass starting at the top and the scrolling to immediately displaying the most recent message at the bottom. This line of code is the closest thing i have found to doing what i want

messagesCollectionView.scrollToLastItem(animated: true)

This is how it's currently displaying and then it will scroll to the bottom when i enter into the conversation (notice date)

enter image description here

This is how i want it do display upon entering the conversation (notice date)

This is

 private func listenForMessages(id: String) {
        Networking.shared.getComments(requestId: 15836, completionHandler: { [weak self] (result) in
            switch result {
            case .success(let comments):
                guard !comments.isEmpty else {
                    return
                }
                
                self?.messages = comments
                DispatchQueue.main.async {
                    self?.messagesCollectionView.reloadData()
                    self?.messagesCollectionView.scrollToLastItem()
                    
                } 
            case .failure(let error):
                print("failed to get messages: \(error)")
            }
    })
        }

2 Answers2

0

You would probably just be able to say animated: false in that line.

goatofanerd
  • 468
  • 2
  • 12
0

You've not posted what you're doing in scrollToLastItem, you can try this though:

 let lastItemIndex = IndexPath(item: collectionArray.count - 1, section: 0)
            self.collectionView.scrollToItem(at: lastItemIndex, at: .bottom, animated: false)
RTXGamer
  • 3,215
  • 6
  • 20
  • 29