IOS - Sticky UICollectionView Header (like Apple App Store)

1k views Asked by At

I am trying to create a header like the one in the App Store. Where when the user drags down, the header stays in place and only the cells scroll down. But when the user drags up, both the header and cells scroll up.

What I have done so far is set my collectionView background view to the image I want to stick to the top and set contentInsets of the collectionView below the image. This way when the user drags down, the image will stay in place. But when the user drags up, my cells go over the image (I want the image to scroll up with the cells). I think I am going about this wrong. Thanks.


 UIView *back = [UIView new];
 UIImageView *backImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 320)];
 backImage.image = [UIImage imageWithData:coverData];
 [back addSubview:backImage];
 self.collectionView.backgroundView = back;

 [self.collectionView setContentInset:UIEdgeInsetsMake(300, 0, 0, 0)];

EDIT I used the tutorial from this question but this does the opposite of what I want, and I am unsure of how to change that code to fit my needs

1

There are 1 answers

0
Peter On BEST ANSWER

Ok I got it. If you are using the tutorial posted from the linked question-

Change

 origin.y = MIN(
                MAX(
                    contentOffset.y,
                    (CGRectGetMinY(firstCellAttrs.frame) - headerHeight)
                ),
                (CGRectGetMaxY(lastCellAttrs.frame) - headerHeight)
            );

To This

origin.y = MIN(
                       contentOffset.y,
                       (CGRectGetMinY(firstCellAttrs.frame) - headerHeight)
                       );