Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display section header on top with UICollectionViewFlowLayout scrolling set to horizontal

This is a duplicate of this question. I'm asking again because the accepted answer is not working and no one's providing more explanation on how the supposed correct answer works.

So here's the situation: I want to display the collection view into one single row. To do this, I applied a custom UICollectionViewFlowLayout to the collection view and set the scrolling to horizontal. Everything is working fine, except for section header disappeared.

Header disappeared

To remedy this, I implemented this function:

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section 
  {
    return CGSizeMake(350, 35);
  }

Now the header is shown, but the problem is it is displayed to the left of the cells, instead of the usual top.

I stumbled upon the link above while searching for a solution, but like I've said, the accepted answer is not working at all and I could not find other solutions about this situation. So can anyone help me here?

Wrong header position

like image 576
Anna Fortuna Avatar asked Jul 30 '14 08:07

Anna Fortuna


1 Answers

we can do that by using the delegate method -

(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section

and keeping the left inset to minus value of width of supplementary view and managing the top inset

like image 64
Mahesh Babu Avatar answered Oct 24 '22 23:10

Mahesh Babu