Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save sourleangchhean168/3c8e330a6000e8c06a0b611be831b3b1 to your computer and use it in GitHub Desktop.
Save sourleangchhean168/3c8e330a6000e8c06a0b611be831b3b1 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.
Swift 3.
*/
func scrollToSection(_ section:Int) {
if let cv = self.collectionView {
let indexPath = IndexPath(item: 1, section: section)
if let attributes = cv.layoutAttributesForSupplementaryElement(ofKind: UICollectionElementKindSectionHeader, at: indexPath) {
let topOfHeader = CGPoint(x: 0, y: 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