How can I set the entire header view to UICollectionView?

后端 未结 1 573
心在旅途
心在旅途 2021-02-08 12:19

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

1条回答
  •  逝去的感伤
    2021-02-08 12:38

    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/

    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.

    Use a supplementary view with NSIndexPath 0-0.

    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.

    0 讨论(0)
提交回复
热议问题