Skip to content

Instantly share code, notes, and snippets.

@CaliosD
Forked from genedelisa/collectionViewScroll
Created March 29, 2016 08:51
Show Gist options
  • Save CaliosD/8522f5982613170dc378 to your computer and use it in GitHub Desktop.
Save CaliosD/8522f5982613170dc378 to your computer and use it in GitHub Desktop.
UICollectionView scroll to make section header visible
/**
Scroll to make the the given section header visible.
The function scrollToItemAtIndexPath will scroll to the item and hide the section header.
*/
func scrollToSection(section:Int) {
if let cv = self.collectionView {
let indexPath = NSIndexPath(forItem: 1, inSection: section)
if let attributes = cv.layoutAttributesForSupplementaryElementOfKind(UICollectionElementKindSectionHeader, atIndexPath: indexPath) {
let topOfHeader = CGPointMake(0, attributes.frame.origin.y - cv.contentInset.top)
cv.setContentOffset(topOfHeader, animated:true)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment