diff --git a/Example/Sources/View Controllers/AnnouncementExampleViewController.swift b/Example/Sources/View Controllers/AnnouncementExampleViewController.swift index 2cbb22c1a..b5bbad7c4 100644 --- a/Example/Sources/View Controllers/AnnouncementExampleViewController.swift +++ b/Example/Sources/View Controllers/AnnouncementExampleViewController.swift @@ -31,6 +31,12 @@ class AnnouncementExampleViewController: MessagesViewController { conversationMessages = ConversationMessageMockFactory.getConversations() } + override func viewDidAppear(_ animated: Bool) { + super.viewDidAppear(animated) + + messagesCollectionView.reloadDataAndKeepOffset() + } + func configureMessageCollectionView() { if let layout = messagesCollectionView.collectionViewLayout as? MessagesCollectionViewFlowLayout { diff --git a/Sources/Views/MessagesCollectionView.swift b/Sources/Views/MessagesCollectionView.swift index f0d515b45..b5fc3de4b 100644 --- a/Sources/Views/MessagesCollectionView.swift +++ b/Sources/Views/MessagesCollectionView.swift @@ -126,6 +126,10 @@ open class MessagesCollectionView: UICollectionView { let newOffset = CGPoint( x: contentOffset.x + (afterContentSize.width - beforeContentSize.width), y: contentOffset.y + (afterContentSize.height - beforeContentSize.height)) + // if offset.y is smaller than contentSize, no need to update contentOffset + guard newOffset.y >= contentSize.height else { + return + } setContentOffset(newOffset, animated: false) }