Skip to content
Open
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
Prev Previous commit
Next Next commit
scrollToLastItem bug fix
  • Loading branch information
hidoon authored Feb 20, 2024
commit c5bf355a5238fa51a9df079a9b606bdd63ec8a91
13 changes: 7 additions & 6 deletions Sources/Views/MessagesCollectionView.swift
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dont think 3 times called scroll function is better

Original file line number Diff line number Diff line change
Expand Up @@ -84,23 +84,24 @@ open class MessagesCollectionView: UICollectionView {
guard let indexPath = indexPathForLastItem else { return }

// Store the current content offset
let originalOffset = collectionView.contentOffset
let originalOffset = self.contentOffset

// Scroll to the item without animation to get the desired offset
collectionView.scrollToItem(at: indexPath, at: pos, animated: false)
let targetOffset = collectionView.contentOffset
self.scrollToItem(at: indexPath, at: pos, animated: false)
let targetOffset = self.contentOffset

// Immediately reset the content offset to the original, without animation
collectionView.setContentOffset(originalOffset, animated: false)
self.setContentOffset(originalOffset, animated: false)

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

// Scroll to the adjusted offset, with or without animation based on the method parameter
collectionView.setContentOffset(adjustedOffset, animated: animated)
self.setContentOffset(adjustedOffset, animated: animated)
}


public func reloadDataAndKeepOffset() {
// stop scrolling
setContentOffset(contentOffset, animated: false)
Expand Down