Programmatically scroll to a supplementary view within UICollectionView

后端 未结 4 1690
没有蜡笔的小新
没有蜡笔的小新 2021-02-07 02:05

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

4条回答
  •  无人共我
    2021-02-07 02:30

    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/

提交回复
热议问题