0

I have a UICollectionView which expands on clicking a cell and once the screen fills it becomes scrollable.

Now when I scroll down I need my header view to scroll down with it and for that I've implemented the logic in the layoutAttributesForSupplementaryViewOfKind method in my custom UICollectionViewLayout class.

This works fine but now the issue is that when I the content becomes scrollable and I scroll down few cells and immediately click on a cell to shrink the content back to one screen at that point the header view doesn't gets arranged, i.e it still remains in the last scrolled position.

But there after if I perform any other action like cell tap it gets arranged properly.

I've tried calling setNeedsLayout, setNeedsDisplay and layoutSubviews where I reload my UICollectionView but the header still doesn't updates to its proper position.

Below is the code for my layoutAttributesForSupplementaryViewOfKind method.

Any help is appreciated.

- (UICollectionViewLayoutAttributes *)layoutAttributesForSupplementaryViewOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {

    if (![kind isEqualToString:[myGridHeaderView kind]]) {
        return nil;
    }

    myGridHeaderPosition headerPosition = [[self collectionView] headerPositionAtIndexPath:indexPath];
    CGRect cellRect = [[self delegate] getRectForHeaderAtIndex:indexPath headerPosition:headerPosition];

    if (CGRectEqualToRect(cellRect, CGRectZero)) {
        return nil;
    }

    myGridHeaderLayoutAttribute* attributes = [myGridHeaderLayoutAttribute layoutAttributesForSupplementaryViewOfKind:kind withIndexPath:indexPath];

    CGPoint centerPoint = CGPointMake(CGRectGetMidX(cellRect), CGRectGetMidY(cellRect));
    CGSize  size        = cellRect.size;


    UICollectionView * const cv = self.collectionView;

    NSInteger zIndex = 1;
    CGPoint const contentOffset = cv.contentOffset;

    if (contentOffset.x > 0)
    {
        if (headerPosition != myGridHeaderPositionColumn)
        {
            centerPoint.x += contentOffset.x;
        }
        zIndex = 1005;
    }
    if (contentOffset.y > 0)
    {
        if (headerPosition != myGridHeaderPositionRow)
        {
            centerPoint.y += contentOffset.y;
        }
        zIndex = 1005;
    }
    if (headerPosition == myGridHeaderPositionCommon) {
        zIndex = 1024;
    }
    attributes.zIndex = zIndex;
    attributes.headerPosition = headerPosition;
    attributes.center = centerPoint;
    attributes.size = size;
    attributes.alpha = 1.0;

    return attributes;

}
Gaurav Patel
  • 532
  • 1
  • 5
  • 19
sonu
  • 41
  • 5

1 Answers1

0

When you scroll up and down , header will be visible and hidden , for use this code.

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    isScrollingStart=YES;
    NSLog(@"scrollViewDidScroll  %f , %f",scrollView.contentOffset.x,scrollView.contentOffset.y);


    if (scrollView.contentOffset.y<=124) {
        _img_top_header.alpha=scrollView.contentOffset.y/124;
    }
    else
    {
        _img_top_header.alpha=1.0;
    }



}

must be set image in header.

Khushal iOS
  • 303
  • 2
  • 12
  • In my case, header is always visible, it is position which is not in correct place. When I check the log the attribute does return the right frame but it still doesn't position is correctly on the collection view until any other action is performed on the collection view. – sonu Jun 20 '18 at 07:50