I'm making simple chat application, so when users taps on specific chat in chat list he must go to last message in this specific chat room (like in Telegram). I tried to create own scrollToBottom function:
func scrollToBottom(animated animated: Bool) {
if self.collectionView.numberOfSections() == 0 {
return
}
if self.collectionView.numberOfItemsInSection(0) == 0 {
return
}
let collectionViewContentHeight = self.collectionView.collectionViewLayout.collectionViewContentSize().height
let isContentTooSmall = collectionViewContentHeight < CGRectGetHeight(self.collectionView.bounds)
if isContentTooSmall {
self.collectionView.scrollRectToVisible(CGRectMake(0, collectionViewContentHeight - 1, 1, 1), animated: animated)
return
}
let finalRow = max(0, self.collectionView.numberOfItemsInSection(0) - 1)
let finalIndexPath = NSIndexPath(forRow: finalRow, inSection: 0)
let finalCellSize = self.collectionView.messagesCollectionViewLayout.sizeForItem(finalIndexPath)
let maxHeightForVisibleMessage = self.collectionView.bounds.height - self.collectionView.contentInset.top
let scrollPosition: UICollectionViewScrollPosition = finalCellSize.height > maxHeightForVisibleMessage ? .Bottom : .Top
self.collectionView.scrollToItemAtIndexPath(finalIndexPath, atScrollPosition: scrollPosition, animated: animated)
}
But when i call it in viewWillAppear i get small delay before it jumps to last row of the chat room, which is not good. Please help!