Skip to content

Commit 28b7b44

Browse files
authored
Improve bounds-change handling for bottomToTop layout (#135)
1 parent 3efa492 commit 28b7b44

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

MagazineLayout/Public/MagazineLayout.swift

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -613,15 +613,22 @@ public final class MagazineLayout: UICollectionViewLayout {
613613

614614
invalidationContext.invalidateLayoutMetrics = false
615615

616-
// If our layout direction is bottom to top we want to adjust scroll position relative to the
617-
// bottom
618-
if
619-
case .bottomToTop = verticalLayoutDirection,
620-
newBounds.maxY < currentCollectionView.contentSize.height + contentInset.bottom
621-
{
622-
invalidationContext.contentOffsetAdjustment = CGPoint(
623-
x: 0.0,
624-
y: currentCollectionView.bounds.maxY - newBounds.maxY)
616+
// If our layout direction is `bottomToTop`, we need to handle the case of a non-animated bounds
617+
// change by using the invalidation context's `contentOffsetAdjustment`. The calculation to get
618+
// this right is odd, and is dependent on how close we were to the bottom of the collection view.
619+
if case .bottomToTop = verticalLayoutDirection {
620+
if newBounds.height < currentCollectionView.bounds.height {
621+
invalidationContext.contentOffsetAdjustment = CGPoint(
622+
x: 0.0,
623+
y: currentCollectionView.bounds.midY - newBounds.midY)
624+
} else if newBounds.height > currentCollectionView.bounds.height {
625+
let distanceFromBottom = currentCollectionView.contentSize.height - currentCollectionView.bounds.maxY
626+
let midYDelta = newBounds.midY - currentCollectionView.bounds.midY
627+
let heightDelta = newBounds.height - currentCollectionView.bounds.height
628+
invalidationContext.contentOffsetAdjustment = CGPoint(
629+
x: 0.0,
630+
y: midYDelta - min(distanceFromBottom, heightDelta))
631+
}
625632
}
626633

627634
return invalidationContext

0 commit comments

Comments
 (0)