【问题标题】:Header view not getting re-arranged after scrolling in UICollectionView在 UICollectionView 中滚动后,标题视图没有重新排列
【发布时间】:2018-11-28 18:03:10
【问题描述】:

我有一个 UICollectionView,它会在单击单元格时展开,一旦屏幕填满,它就会变成可滚动的。

现在,当我向下滚动时,我需要我的标题视图随之向下滚动,为此我在我的自定义 UICollectionViewLayout 类中实现了 layoutAttributesForSupplementaryViewOfKind 方法中的逻辑。

这很好,但现在的问题是,当我的内容变得可滚动并且我向下滚动几个单元格并立即单击一个单元格以将内容缩小回一个屏幕时,标题视图没有得到安排,即它仍然保持在最后滚动的位置。

但是之后,如果我执行任何其他操作,例如单元格点击,它就会得到正确安排。

我尝试调用setNeedsLayoutsetNeedsDisplaylayoutSubviews 重新加载我的 UICollectionView 但标题仍然没有更新到正确的位置。

下面是我的layoutAttributesForSupplementaryViewOfKind 方法的代码。

感谢任何帮助。

- (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;

}

【问题讨论】:

    标签: ios objective-c uicollectionview uicollectionviewlayout xcode9.3


    【解决方案1】:

    当您上下滚动时,标题将可见和隐藏,供使用此代码。

    - (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;
        }
    
    
    
    }
    

    必须在标题中设置图片。

    【讨论】:

    • 在我的情况下,标题总是可见的,它是位置不正确的位置。当我检查日志时,该属性确实返回了正确的框架,但在集合视图上执行任何其他操作之前,它仍然没有正确定位在集合视图上。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-21
    • 2014-09-23
    • 1970-01-01
    相关资源
    最近更新 更多