gpt4 book ai didi

iphone - 如何使补充 View 在 UICollectionView 中 float ,就像 UITableView 普通样式中的节标题一样

转载 作者:行者123 更新时间:2023-12-03 18:09:11 24 4
gpt4 key购买 nike

我正在努力使用 UICollectionView 实现“ float 节标题”效果。如果没有大量的努力,在 UITableView 中很简单的事情(UITableViewStylePlain 的默认行为)在 UICollectionView 中似乎是不可能的。我错过了显而易见的事情吗?

Apple 没有提供有关如何实现此目的的文档。似乎必须子类化 UICollectionViewLayout 并实现自定义布局才能实现此效果。这需要大量的工作,实现以下方法:

重写方法

每个布局对象都应该实现以下方法:

collectionViewContentSize
layoutAttributesForElementsInRect:
layoutAttributesForItemAtIndexPath:
layoutAttributesForSupplementaryViewOfKind:atIndexPath: (if your layout supports supplementary views)
layoutAttributesForDecorationViewOfKind:atIndexPath: (if your layout supports decoration views)
shouldInvalidateLayoutForBoundsChange:

但是,我不清楚如何使补充 View float 在单元格上方并“粘贴”到 View 顶部,直到到达下一部分。布局属性中是否有一个标志?

我会使用UITableView,但我需要创建一个相当复杂的集合层次结构,这可以通过 Collection View 轻松实现。

任何指导或示例代码将不胜感激!

最佳答案

在 iOS9 中,Apple 很友善地在 UICollectionViewFlowLayout 中添加了一个简单的属性。叫sectionHeadersPinToVisibleBounds .

这样,您就可以使标题像表格 View 中那样 float 。

let layout = UICollectionViewFlowLayout()
layout.sectionHeadersPinToVisibleBounds = true
layout.minimumInteritemSpacing = 1
layout.minimumLineSpacing = 1
super.init(collectionViewLayout: layout)

关于iphone - 如何使补充 View 在 UICollectionView 中 float ,就像 UITableView 普通样式中的节标题一样,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13511733/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com