Ios 如何创建跨越多个节的粘性标题
我有一个集合视图,其中每个单元格都是一个部分。我需要实现一个“粘性头”,它将跨越3个部分,直到另一个“粘性头”取代它。我该怎么做?由于其他原因,我无法将我的单元格更改为全部共享同一分区。我将仅在第一个分区演示如何执行此操作。因为使用了一些常量,所以它是相当不成熟的。然而,如果你遵循这个模式,你可以更容易地实现你的目标 为了达到高精度,请随意更改代码 基本上,只需将UICollectionViewFlowLayout子类化Ios 如何创建跨越多个节的粘性标题,ios,swift,Ios,Swift,我有一个集合视图,其中每个单元格都是一个部分。我需要实现一个“粘性头”,它将跨越3个部分,直到另一个“粘性头”取代它。我该怎么做?由于其他原因,我无法将我的单元格更改为全部共享同一分区。我将仅在第一个分区演示如何执行此操作。因为使用了一些常量,所以它是相当不成熟的。然而,如果你遵循这个模式,你可以更容易地实现你的目标 为了达到高精度,请随意更改代码 基本上,只需将UICollectionViewFlowLayout子类化 class MyCollectionViewFlowLayout: UIC
class MyCollectionViewFlowLayout: UICollectionViewFlowLayout{
lazy var offset : CGFloat? = { return self.collectionView?.contentOffset.y}()
// only first section here, you need to add other sections programmatically later.
override func invalidationContext(forBoundsChange newBounds: CGRect) -> UICollectionViewLayoutInvalidationContext{
let result = super.invalidationContext(forBoundsChange: newBounds)
result.invalidateSupplementaryElements(ofKind: UICollectionView.elementKindSectionHeader, at: [IndexPath.init(row: 0, section: 0)])
return result
}
override func layoutAttributesForSupplementaryView(ofKind elementKind: String, at indexPath: IndexPath) -> UICollectionViewLayoutAttributes?{
let result = super.layoutAttributesForSupplementaryView(ofKind: elementKind, at: indexPath)
guard indexPath.section % 3 == 0 else {
return result
}
let section = CGFloat (indexPath.section / 3)
let totalHeight : CGFloat = 414.0 + 1390.0 * (section) //414 = first section height
// 1390 = height of three sections
let totalNextHight : CGFloat = 1390.0 * (section + 1)
if let result = result {
if result.frame.origin.y > CGFloat(totalHeight) {
result.frame = CGRect.init(x: result.frame.origin.x , y: (result.frame.origin.y < totalNextHight) ? (self.collectionView?.contentOffset.y)! - offset! + totalHeight : totalNextHight, width: result.frame.width, height: result.frame.height)
}
}
return result
}
}
这些代码背后有很多工作要做。希望你能尽快找到自己的答案
extension MyCollectionViewController: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize{
if section % 3 == 0 {return CGSize.init(width:200 , height: 44)}
return CGSize.init(width:0 , height: 0)
}