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

标签 iphone uitableview view uicollectionview supplementary

我正在努力使用 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/

相关文章:

ios - 在 Xcode 6 中通过 Storyboard 设置 UITableView 的 contentInset

ios - Ios UITableview 中的自适应表格 View 单元格

java - 为什么我的 View 数组返回 null?

iphone - iOS从 View Controller 访问 View 的变量

iphone - TableView : Data overlapped while delete the row

iphone - 中断 iPhone IOS 5 上的通话/短信

iphone - 使用动态行高时 UITableView 出现间隙

java - 以 3x3 模式排列屏幕

ios - ios录制视频时如何检测设备的移动速度

ios - UIBezierPath:在 iPad 上创建的控制点多于 iPhone?