Last active
April 11, 2018 13:38
-
-
Save sourleangchhean168/d177559c19e73583932f2e64306ef123 to your computer and use it in GitHub Desktop.
CollectionView Move to Section Swift 4.1
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
//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