I am using UICollectionView
to display photos in sections. Each section has a supplementary view as a header and is supplied via the method: viewForSupplement
Solution in Swift,
let section: Int = 0 // Top
if let cv = self.collectionView {
cv.layoutIfNeeded()
let indexPath = IndexPath(row: 1, section: section)
if let attributes = cv.layoutAttributesForSupplementaryElement(ofKind: UICollectionView.elementKindSectionHeader, at: indexPath) {
let topOfHeader = CGPoint(x: 0, y: attributes.frame.origin.y - cv.contentInset.top)
cv.setContentOffset(topOfHeader, animated: true)
}
}
Props to Gene De Lisa: http://www.rockhoppertech.com/blog/scroll-to-uicollectionview-header/