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 scrollToItem appearing underneath sticky header #279

Merged
merged 2 commits into from
Mar 12, 2021
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

### Fixed

- When calling `scrollToItem` with a `.top` scroll position, [the item no longer appears underneath sticky section headers](https://github.com/kyleve/Listable/pull/279).

### Added

- [Adds `scrollToSection`](https://github.com/kyleve/Listable/pull/277) to `ListActions` and `ListView`. To support this functionality, `Section` can now be queried with an `Identifier`. Also added `SectionPosition` to specify the top or bottom within a `Section`.
Expand Down
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