简体   繁体   English

在UICollectionView中滚动后,标题视图未重新排列

[英]Header view not getting re-arranged after scrolling in UICollectionView

I have a UICollectionView which expands on clicking a cell and once the screen fills it becomes scrollable. 我有一个UICollectionView,它会在单击一个单元格时扩展,并且一旦屏幕填满,它就可以滚动。

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. 现在,当我向下滚动时,我需要标题视图向下滚动,为此,我已经在自定义UICollectionViewLayout类的layoutAttributesForSupplementaryViewOfKind方法中实现了逻辑。

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, ie 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. 我尝试调用setNeedsLayoutsetNeedsDisplaylayoutSubviews ,在其中重新加载UICollectionView,但标头仍未更新为正确位置。

Below is the code for my layoutAttributesForSupplementaryViewOfKind method. 以下是我的layoutAttributesForSupplementaryViewOfKind方法的代码。

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;

}

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. 必须在标题中设置图片。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM