17

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

Community
  • 1
  • 1
Anna Fortuna
  • 1,071
  • 2
  • 17
  • 33
  • I am facing the same problem now. How could you manage it? If you have to solution please share with me. Thanks – Tulon Dec 14 '14 at 06:50
  • Hi Tulon. Unfortunately, I wasn't able to find any solution for this. I decided to create a custom view instead. – Anna Fortuna Dec 15 '14 at 08:17
  • Hi Anna. I managed this with an alternative solution. I did it with multiple `UICollectionView`. It's easy to do and handy a lot. :) – Tulon Dec 15 '14 at 08:39

2 Answers2

1

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

Dharmesh Dhorajiya
  • 3,976
  • 9
  • 30
  • 39
-1

Have you tried using the header with something like this?

First: set it up in viewDidLoad...

UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
flowLayout.headerReferenceSize = CGSizeMake(self.collectionView.bounds.size.width, 30);
// add any other setup you need
[self.collectionView setCollectionViewLayout:flowLayout];

Second: add header view ...

#define LABEL_TAG 128

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {

    UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:
                                            UICollectionElementKindSectionHeader withReuseIdentifier:@"SectionHeader" forIndexPath:indexPath];
    UILabel *label = (UILabel *)[headerView viewWithTag:LABEL_TAG];
    if (!label) {
        label = [[UILabel alloc] initWithFrame:CGRectInset(headerView.bounds, 5, 5)];
        label.tag = MY_HEADER_LABEL_TAG;
        label.font = [UIFont boldSystemFontOfSize:12];
        label.textColor = [UIColor redColor];
        [headerView addSubview:label];
    }

    label.text = [NSString stringWithFormat:@"Section %d", indexPath.section];
    return headerView;
}
mvien
  • 237
  • 4
  • 12