Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Added scrollToBottom() method in MessagesCollectionView
  • Loading branch information
Piggyback13 committed Dec 10, 2024
commit abed2ab90c242f85c1fb667c30bd93fe32503a4e
15 changes: 15 additions & 0 deletions Sources/Views/MessagesCollectionView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,21 @@ open class MessagesCollectionView: UICollectionView {

scrollToItem(at: indexPath, at: pos, animated: animated)
}

public func scrollToBottom(animated: Bool = true) {
guard let indexPath = indexPathForLastItem else { return }

// scroll to the item without animation to get the desired offset
scrollToItem(at: indexPath, at: .bottom, animated: false)
let targetOffset = contentOffset

// calculate the adjusted offset, considering the bottom section inset
let sectionInsetBottom = (collectionViewLayout as? UICollectionViewFlowLayout)?.sectionInset.bottom ?? 0
let adjustedOffset = CGPoint(x: targetOffset.x, y: targetOffset.y + sectionInsetBottom)

// set content offset to the adjusted offset
setContentOffset(adjustedOffset, animated: animated)
}

public func reloadDataAndKeepOffset() {
// stop scrolling
Expand Down