Skip to content

Commit

Permalink
Merge pull request #623 from badoo/fix-scrolled-messages-after-frame-…
Browse files Browse the repository at this point in the history
…change

Use bounds height change in adjustCollectionViewInsets
  • Loading branch information
leonspok authored Nov 5, 2019
2 parents dbfacce + bd2bb87 commit 6cb0868
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion Chatto/Source/ChatController/BaseChatViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ open class BaseChatViewController: UIViewController, UICollectionViewDataSource,
return availableHeight >= contentSize.height
}

private var previousBoundsUsedForInsetsAdjustment: CGRect? = nil
func adjustCollectionViewInsets(shouldUpdateContentOffset: Bool) {
guard let collectionView = self.collectionView else { return }
let isInteracting = collectionView.panGestureRecognizer.numberOfTouches > 0
Expand All @@ -317,10 +318,22 @@ open class BaseChatViewController: UIViewController, UICollectionViewDataSource,

let prevContentOffsetY = collectionView.contentOffset.y

let boundsHeightDiff: CGFloat = {
guard shouldUpdateContentOffset, let lastUsedBounds = self.previousBoundsUsedForInsetsAdjustment else {
return 0
}
let diff = lastUsedBounds.height - collectionView.bounds.height
// When collectionView is scrolled to bottom and height increases,
// collectionView adjusts its contentOffset automatically
let isScrolledToBottom = contentSize.height <= collectionView.bounds.maxY - collectionView.contentInset.bottom
return isScrolledToBottom ? max(0, diff) : diff
}()
self.previousBoundsUsedForInsetsAdjustment = collectionView.bounds

let newContentOffsetY: CGFloat = {
let minOffset = -newInsetTop
let maxOffset = contentSize.height - (collectionView.bounds.height - newInsetBottom)
let targetOffset = prevContentOffsetY + insetBottomDiff
let targetOffset = prevContentOffsetY + insetBottomDiff + boundsHeightDiff
return max(min(maxOffset, targetOffset), minOffset)
}()

Expand Down

0 comments on commit 6cb0868

Please sign in to comment.