如何像 UITableView 的 tableHeaderView 一样在 UICollectionView 中添加 HeaderView [英] How to add HeaderView in UICollectionView like UITableView's tableHeaderView
问题描述
如何在 UICollectionView
的顶部添加标题视图/顶视图(不是部分标题)?
How can I add a header view / top view (not section header) at the top of a UICollectionView
?
它应该完全作为 UITableView
的 tableHeaderView
属性.
It should act excactly as UITableView
's tableHeaderView
property.
所以它需要位于第一个部分标题视图的顶部(在索引 0 的部分之前),与其余内容一起滚动,并进行用户交互.
So it needs to sit on top of the first section header view (before the section at index 0), scroll along with the rest of the content, and have user interaction.
到目前为止,我想出的最好的方法是为第一节标题视图制作一个特殊的 XIB(UICollectionReusableView
的 MyCollectionReusableView
子类作为文件的所有者)这足够大,可以在标题中包含我的子视图,我认为这是一种黑客行为,而且我还没有设法检测到触摸.
The best I've come up with so far is to make a special XIB (with MyCollectionReusableView
subclass of UICollectionReusableView
as the File's owner) for the first section header view that is big enough to also contain my subviews in header, it's kind of a hack, I think, and I haven't managed to detect touches.
不确定我是否可以让我的 MyCollectionReusableView
子类允许触摸或有更好的方法.
Not sure if I can make my MyCollectionReusableView
subclass to allow touches or there's a better way.
推荐答案
我通过添加一个 UIView 作为集合视图的子视图,将视图放在集合视图的顶部.它确实与集合视图一起向上滚动,并且具有正常的 UIView 用户交互.如果您没有节标题,这可以正常工作,但如果您这样做则不起作用.在那种情况下,我认为你的做法没有任何问题.我不确定你为什么没有检测到触摸,它们对我来说很好.
I've put a view at the top of a collection view by just adding a UIView as a subview of the collection view. It does scroll up with the collection view and it has normal UIView user interaction. This works ok if you have no section header, but doesn't work if you do. In that situation, I don't see anything wrong with the way you're doing it. I'm not sure why you're not detecting touches, they work fine for me.
这篇关于如何像 UITableView 的 tableHeaderView 一样在 UICollectionView 中添加 HeaderView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!