Skip to content

Commit

Permalink
Fix scrollToItem appearing underneath sticky header
Browse files Browse the repository at this point in the history
When a `top` scroll position is used, the item will appear underneath the sticky section header.
Now, we adjust the frame and use a manual content offset when sticky headers are enabled.
When sticky headers are disabled or scrolling to a different position, we call the existing `scrollToItem` function on UICollectionView.
  • Loading branch information
lokae0 committed Mar 12, 2021
1 parent 24559be commit 432a9dc
Showing 1 changed file with 26 additions and 9 deletions.
35 changes: 26 additions & 9 deletions ListableUI/Sources/ListView/ListView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -424,27 +424,44 @@ public final class ListView : UIView, KeyboardObserverDelegate
}

return self.preparePresentationStateForScroll(to: toIndexPath) {

let isAlreadyVisible: Bool = {
let frame = self.collectionViewLayout.frameForItem(at: toIndexPath)
let itemFrame = self.collectionViewLayout.frameForItem(at: toIndexPath)

return self.collectionView.contentFrame.contains(frame)
}()
let isAlreadyVisible = self.collectionView.contentFrame.contains(itemFrame)

// If the item is already visible and that's good enough, return.

if isAlreadyVisible && position.ifAlreadyVisible == .doNothing {
return
}

animation.perform(
animations: {

let scroll: () -> Void = {
let sectionHeader = self.collectionViewLayout.layout.content.sections[toIndexPath.section].header

// Prevent the item from appearing underneath a sticky section header.

if sectionHeader.isPopulated,
self.collectionViewLayout.layout.stickySectionHeaders,
position.position == .top {

let itemFrameAdjustedForStickyHeaders = CGRect(
x: itemFrame.minX,
y: itemFrame.minY - sectionHeader.size.height,
width: itemFrame.width,
height: itemFrame.height
)
self.performScroll(to: itemFrameAdjustedForStickyHeaders, scrollPosition: position)

} else {
self.collectionView.scrollToItem(
at: toIndexPath,
at: position.position.UICollectionViewScrollPosition,
animated: false
)
},
}
}

animation.perform(
animations: scroll,
completion: completion
)
}
Expand Down

0 comments on commit 432a9dc

Please sign in to comment.