Skip to content

Instantly share code, notes, and snippets.

@sourleangchhean168
Last active April 11, 2018 13:38
Show Gist options
  • Save sourleangchhean168/d177559c19e73583932f2e64306ef123 to your computer and use it in GitHub Desktop.
Save sourleangchhean168/d177559c19e73583932f2e64306ef123 to your computer and use it in GitHub Desktop.
CollectionView Move to Section Swift 4.1
//Swift 4.1
func moveToSection(section:Int){
if let sec = self.collectionView?.numberOfSections{
if sec > section{
let indexP = IndexPath(item: 0, section: section)
if let cv = self.collectionView{
if section == 0{
cv.scrollToItem(at: indexP, at: .centeredVertically, animated: true)
}else{
if let attributes = cv.layoutAttributesForSupplementaryElement(ofKind: UICollectionElementKindSectionHeader, at: indexP) {
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