3

In my UICollectionViewFlowLayout subclass I have this:

self.headerReferenceSize = CGSizeMake(280, 44);

However the header is displayed with a width of 320, which is the width of the collection view. This is correct according to the docs:

During layout, only the size that corresponds to the appropriate scrolling direction is used. For example, for the vertical scrolling direction, the layout object uses the height value returned by your method. (In that instance, the width of the header would be set to the width of the collection view.)

However, I need the width to be 280, and not stretch to the entire width of the collection view. How can I override this?

soleil
  • 11,239
  • 29
  • 102
  • 172

2 Answers2

0

You can add another custom view over header and resize it as you wish. Ofcourse all you're components that were on header will go over custom view you created...

saigen
  • 186
  • 1
  • 8
0

You don't need to add a subview to the header.

You want a header width of 280 in a Collection View of 320, so apply a Content Inset of 20 on the left and right, like so:

Swift

YourCollectionView.ContentInset = UIEdgeInsets(top: 0, left: 20, bottom: 20, right: 0)

Xamarin

YourCollectionView.ContentInset = new UIEdgeInsets(0, 20, 0, 20);
Chucky
  • 1,706
  • 6
  • 27
  • 58