Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix more target content offset edge cases #118

Merged
merged 1 commit into from
Feb 16, 2024
Merged
Changes from all commits
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
22 changes: 13 additions & 9 deletions MagazineLayout/Public/MagazineLayout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -239,11 +239,21 @@ public final class MagazineLayout: UICollectionViewLayout {

// Calculate the target offset before applying updates, since the target offset should be based
// on the pre-update state.
targetContentOffsetAnchor = targetContentOffsetAnchor(
let proposedTargetContentOffsetAnchor = targetContentOffsetAnchor(
bounds: currentCollectionView.bounds,
contentHeight: collectionViewContentSize.height,
topInset: contentInset.top,
bottomInset: contentInset.bottom)
switch proposedTargetContentOffsetAnchor {
case .top, .bottom:
targetContentOffsetAnchor = proposedTargetContentOffsetAnchor
case .topItem, .bottomItem:
// Preserving the content offset when we're not fully at the top or bottom is tricky when
// inserting / deleting items, so we don't even attempt to do it for now.
break
case .none:
break
}

modelState.applyUpdates(updates)
hasDataSourceCountInvalidationBeforeReceivingUpdateItems = false
Expand Down Expand Up @@ -728,12 +738,10 @@ public final class MagazineLayout: UICollectionViewLayout {

let isScrolling = currentCollectionView.isDragging || currentCollectionView.isDecelerating
let top = currentCollectionView.bounds.minY + contentInset.top
let bottom = currentCollectionView.bounds.maxY
let isSizingElementAboveTopEdge = originalAttributes.frame.minY < top
let isSizingElementBelowBottomEdge = originalAttributes.frame.minY > bottom

let contentOffsetAdjustment: CGPoint
if verticalLayoutDirection == .topToBottom, isSizingElementAboveTopEdge {
if isSizingElementAboveTopEdge, isScrolling {
// If layout information is discarded above our current scroll position (on rotation, for
// example), we need to compensate for preferred size changes to items as we're scrolling up,
// otherwise, the collection view will appear to jump each time an element is sized.
Expand All @@ -760,11 +768,7 @@ public final class MagazineLayout: UICollectionViewLayout {
x: 0,
y: preferredAttributes.size.height - originalAttributes.size.height)
}
} else if
verticalLayoutDirection == .bottomToTop,
!isSizingElementBelowBottomEdge,
modelState.isPerformingBatchUpdates
{
} else if targetContentOffsetAnchor == .bottom {
contentOffsetAdjustment = CGPoint(
x: 0,
y: preferredAttributes.size.height - originalAttributes.size.height)
Expand Down
Loading