Horizontal UICollectionViewLayout with Sticky HeaderView
- iOS9+
- tvOS9+
Just implement these 5 required delegate methods.
extensionViewController:HorizontalStickyHeaderLayoutDelegate{privateenumConst{staticletheaderSize=CGSize(width:100, height:38)staticletitemSize0=CGSize(width:50, height:50)staticletitemSize1=CGSize(width:80, height:80)staticletheaderLeft:CGFloat=8}func collectionView(_ collectionView:UICollectionView, hshlSizeForItemAtIndexPath indexPath:IndexPath)->CGSize{if indexPath.section %2==0{returnConst.itemSize0
}else{returnConst.itemSize1
}}func collectionView(_ collectionView:UICollectionView, hshlSizeForHeaderAtSection section:Int)->CGSize{returnConst.headerSize
}func collectionView(_ collectionView:UICollectionView, hshlHeaderInsetsAtSection section:Int)->UIEdgeInsets{returnUIEdgeInsets(top:0, left:Const.headerLeft, bottom:20, right:20)}func collectionView(_ collectionView:UICollectionView, hshlMinSpacingForCellsAtSection section:Int)->CGFloat{return20}func collectionView(_ collectionView:UICollectionView, hshlSectionInsetsAtSection section:Int)->UIEdgeInsets{returnUIEdgeInsets(top:0, left:0, bottom:0, right: section ==4?0:20)}}Optionally you can define contentInset for outer margin.
See Example for detail.
- On focus, call
updatePoppingHeaderIndexPaths()to recalculate the popping header indexPaths to get the latest indexPaths. - Listen to pop indexPaths change on scroll by implementing
collectionView(_:,hshlDidUpdatePoppingHeaderIndexPaths:)delegate method. - animate container view of your header view.
See Example for recommended implementation.
// Either in UICollectionViewDelegate or this override method.
overridefunc didUpdateFocus(in context:UIFocusUpdateContext, with coordinator:UIFocusAnimationCoordinator){
layout.updatePoppingHeaderIndexPaths()let(pop, unpop)=self.getHeaders(poppingHeadersIndexPaths:self.layout.poppingHeaderIndexPaths)UIView.animate(withDuration:Const.unpopDuration, delay:0, options:[.curveEaseOut], animations:{
unpop.forEach{ $0.unpopHeader()}}, completion:nil)
coordinator.addCoordinatedAnimations({
pop.forEach{ $0.popHeader()}}, completion:nil)
super.didUpdateFocus(in: context, with: coordinator)}func collectionView(_ collectionView:UICollectionView, hshlDidUpdatePoppingHeaderIndexPaths indexPaths:[IndexPath]){let(pop, unpop)=self.getHeaders(poppingHeadersIndexPaths:self.layout.poppingHeaderIndexPaths)UIView.animate(withDuration:Const.unpopDuration, delay:0, options:[.curveEaseOut], animations:{
unpop.forEach{ $0.unpopHeader()}
pop.forEach{ $0.popHeader()}}, completion:nil)}Install via Xcode.
pod "HorizontalStickyHeaderLayout"
MIT


