28

I am creating a UICollectionView which scrolls horizontally but with a vertical section header.

My problem is that for horizontal scrolling, the section header defaults to the left side of the collection view and I cannot adjust it to the top of the UICollectionView.

I have checked out various solutions which includes DateFlowLayout which unfortunately does not work anymore.

enter image description here

Wraithseeker
  • 1,884
  • 2
  • 19
  • 34
  • check this http://stackoverflow.com/questions/31309918/application-crash-when-set-header-of-uicollectionview-horizontally/31309991?noredirect=1#comment50613185_31309991 may help – Kumar Jul 09 '15 at 11:22
  • I am also facing this issue , i cant believe its such a common problem and there is no api with UIcollectionViewFlowlayout to resolve this issue – Ajay Beniwal Jun 17 '17 at 11:20
  • This pod did it for me: https://github.com/toshi0383/HorizontalStickyHeaderLayout – d4Rk Aug 07 '19 at 10:18
  • I found it easier to place the collection view into a table view cell. That allows you to use the table view section headers. – mtl Aug 06 '22 at 22:31

1 Answers1

5

You have to implement your own collection view layout to do so.

The easiest would be to subclass UICollectionViewFlowLayout and override

- (UICollectionViewLayoutAttributes *)layoutAttributesForSupplementaryViewOfKind:(NSString *)elementKind atIndexPath:(NSIndexPath *)indexPath;

to give the frame you wish for the header

Jaeger
  • 324
  • 1
  • 10
  • I don't want to edit the bounds of the header but just the header position – Wraithseeker Jul 09 '15 at 11:57
  • Sorry, I meant the frame, position, size,... whatever you want :) Please read the documentation for for info https://developer.apple.com/library/ios/documentation/UIKit/Reference/UICollectionViewLayoutAttributes_class/ – Jaeger Jul 09 '15 at 11:59
  • I have been searching around the internet and also the doc but their usage seems to be very basic and does not cover section header at all. I am lost as to how to proceed with that method. How do I reference my section header in that method? – Wraithseeker Jul 09 '15 at 14:00
  • @chiken if you find the solution please share it I am facing the same problem. thank you – Niib Fouda Mar 06 '16 at 20:36
  • 1
    @NiibFouda this might be helpful: http://stackoverflow.com/questions/17826740/uicollectionview-special-horizontal-flow-multiple-sections – Ayazmon Feb 03 '17 at 13:24