需求:UICollectionView 的 Section Header 在滑動時懸浮於頂部,類似 UITableView 的 sectionHeaderTopPadding 效果。iOS 原生不支持,需引入第三方庫。
GitHub: https://github.com/derrh/StickyViews
pod 'StickyViews'StickyCollectionViewFlowLayout 而非 UICollectionViewFlowLayoutviewForSupplementaryElementOfKind 返回 Header Viewlet layout = StickyCollectionViewFlowLayout()
layout.stickyHeaders = true // 啟用懸浮
let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
CSStickyHeaderFlowLayout (ObjC 經典庫)UICollectionViewCompositionalLayout 手動實現(代碼量大)StickyViews 維護活躍、Swift 友好、支持動態高度 Header,首選。