Created
October 22, 2018 14:06
-
-
Save pmatanyc/31eef6ede28658014c41b4d51c8bcefc to your computer and use it in GitHub Desktop.
Scrolling to a section with a sticky header in UICollectionView
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func scrollToSection(_ section: Int) { | |
if let collectionView = collectionView { | |
let indexPath = IndexPath(item: 0, section: section) | |
if | |
let attributes = collectionView.layoutAttributesForItem(at: indexPath), | |
let flowLayout = collectionView.collectionViewLayout as? FlowLayout | |
{ | |
let headerHeight = flowLayout.headerReferenceSize | |
let topSectionInset = flowLayout.sectionInset.top | |
let topOfHeader = CGPoint(x: 0, y: attributes.frame.minY - headerHeight.height - topSectionInset) | |
collectionView.setContentOffset(topOfHeader, animated: true) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment