Skip to content

Commit abed2ab

Browse files
committed
Added scrollToBottom() method in MessagesCollectionView
1 parent bf24e05 commit abed2ab

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

Sources/Views/MessagesCollectionView.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,21 @@ open class MessagesCollectionView: UICollectionView {
8585

8686
scrollToItem(at: indexPath, at: pos, animated: animated)
8787
}
88+
89+
public func scrollToBottom(animated: Bool = true) {
90+
guard let indexPath = indexPathForLastItem else { return }
91+
92+
// scroll to the item without animation to get the desired offset
93+
scrollToItem(at: indexPath, at: .bottom, animated: false)
94+
let targetOffset = contentOffset
95+
96+
// calculate the adjusted offset, considering the bottom section inset
97+
let sectionInsetBottom = (collectionViewLayout as? UICollectionViewFlowLayout)?.sectionInset.bottom ?? 0
98+
let adjustedOffset = CGPoint(x: targetOffset.x, y: targetOffset.y + sectionInsetBottom)
99+
100+
// set content offset to the adjusted offset
101+
setContentOffset(adjustedOffset, animated: animated)
102+
}
88103

89104
public func reloadDataAndKeepOffset() {
90105
// stop scrolling

0 commit comments

Comments
 (0)