Skip to content

Instantly share code, notes, and snippets.

@pmatanyc
Created October 22, 2018 14:06
Show Gist options
  • Save pmatanyc/31eef6ede28658014c41b4d51c8bcefc to your computer and use it in GitHub Desktop.
Save pmatanyc/31eef6ede28658014c41b4d51c8bcefc to your computer and use it in GitHub Desktop.
Scrolling to a section with a sticky header in UICollectionView
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