如何将整个标题视图设置为UICollectionView? [英] How can I set the entire header view to UICollectionView?

查看:92
本文介绍了如何将整个标题视图设置为UICollectionView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将标题设置为集合视图,就像UITableViewController的setTableHeader一样。

I want to set the header to collection view, just like UITableViewController's setTableHeader does.

我找到了在集合视图中设置每个部分标题的方法,但是我无法找到如何设置整个标题的标题视图。

I found the way to set the each section's header on collection view, but I couldn't find how to set the header view of the entire header.

Apple对UICollectionViewLayout的引用说:补充视图显示数据但不同于单元格。与单元格不同,补充用户无法选择视图。您可以使用补充视图来实现给定部分或整个集合视图的页眉和页脚视图等内容。补充视图是可选的,它们的使用和放置由布局对象定义。 ,但我不知道如何为整个集合视图实现标题视图。

Apple's reference of UICollectionViewLayout says "Supplementary views present data but are different than cells. Unlike cells, supplementary views cannot be selected by the user. Instead, you use supplementary views to implement things like header and footer views for a given section or for the entire collection view. Supplementary views are optional and their use and placement is defined by the layout object.", but I don't know how to implement header view for the entire collection view.

我尝试通过仅将节标题视图矩形设置为第一部分来实现此目的,但与tableHeader的不同之处在于。

I tried to implement this by making a section header view rectangle only to the first section, but it was different with tableHeader's that.

在表格视图中,如果我设置内容关闭在下面的标题中,内容大小会自动扩展,以便隐藏标题,尽管存在一些元素。

In table view, if I set the content offset to below header, the content size automatically expands so that the header is hidden although there exist a few elements.

但是在集合视图中,修改内容偏移仅适用于存在许多足以进行滚动的元素。

But in collection view, modifying content offset only works fine when there exist many elements enough to make a scroll.

当存在少量元素时,我试图动态扩展内容大小,但似乎它不能正常工作。 ..

I tried to dynamically expand content size when there exist a few elements, but it seems that it doesn't work fine...

推荐答案

我使用自定义布局实现了这一点,并在每个滚动事件上重新定位标题。从子类化UICollectionViewLayout开始,网上有大量的教程,这是一个很好的教程: http://markpospesel.wordpress.com/2012/10/25/fixing-circlelayout/

I implemented this using a custom layout and repositioning the header on every scroll event. Start with subclassing UICollectionViewLayout, there are tons of tutorial on the web, this is a good one: http://markpospesel.wordpress.com/2012/10/25/fixing-circlelayout/

然后诀窍是始终返回 YES shouldInvalidateLayoutForBoundsChange:,并在 prepareLayout 重新定位(即更改) layoutAttributes'框架)基于集合视图xOffset和yOffset的标题。

Then the trick is to always return YES to shouldInvalidateLayoutForBoundsChange:, and in prepareLayout reposition (ie. change the layoutAttributes' frame) the header based on the collection view xOffset and yOffset.

使用NSIndexPath 0-0的补充视图。

Use a supplementary view with NSIndexPath 0-0.

layoutAttributesForElementsInRect:将在每个帧上调用,因此如果您的集合视图中有很多项,则需要缓存结果以便它不会影响性能。除了那一个标题之外,所有属性都应该不受影响。

layoutAttributesForElementsInRect: will be called on every frame, so if you have a lot of items in your collection view, you will need to cache the result so it does not impact performance. All attributes should be untouched except for that one header.

资料来源:我花了一个月的时间研究这一特定主题。

Source: I spent 1 month working on this particular topic.

这篇关于如何将整个标题视图设置为UICollectionView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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