如何在UICollectionView中将Supplementary View float作为Section Headers在UITableView普通样式中进行 [英] How to make Supplementary View float in UICollectionView as Section Headers do in UITableView plain style

查看:235
本文介绍了如何在UICollectionView中将Supplementary View float作为Section Headers在UITableView普通样式中进行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难用 UICollectionView 来实现浮动节头效果。 UITableView UITableViewStylePlain 的默认行为)似乎不可能在 UICollectionView 没有多少辛苦工作。我错过了明显的吗?

I'm struggling to achieve a "floating section header" effect with UICollectionView. Something that's been easy enough in UITableView (default behavior for UITableViewStylePlain) seems impossible in UICollectionView without lots of hard work. Am I missing the obvious?

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

Apple provides no documentation on how to achieve this. It seems that one has to subclass UICollectionViewLayout and implement a custom layout just to achieve this effect. This entails quite a bit of work, implementing the following methods:

覆盖的方法

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

Every layout object should implement the following methods:

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

然而,我不清楚如何使补充视图浮动到单元格上方并粘贴到视图的顶部,直到到达下一部分。布局属性中是否有这样的标志?

However its not clear to me how to make the supplementary view float above the cells and "stick" to the top of the view until the next section is reached. Is there a flag for this in the layout attributes?

我会使用 UITableView 但我需要创建一个相当复杂的集合层次结构,这很容易用集合视图。

I would have used UITableView but I need to create a rather complex hierarchy of collections which is easily achieved with a collection view.

非常感谢任何指导或示例代码!

Any guidance or sample code would be greatly appreciated!

推荐答案

在iOS9中,Apple非常友好地在 UICollectionViewFlowLayout 名为 sectionHeadersPinToVisibleBounds

In iOS9, Apple was kind enough to add a simple property in UICollectionViewFlowLayout called sectionHeadersPinToVisibleBounds.

这样,您可以使表格视图中的标题浮动。

With this, you can make the headers float like that in table views.

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

这篇关于如何在UICollectionView中将Supplementary View float作为Section Headers在UITableView普通样式中进行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆