【问题标题】:UICollectionViewFlowLayout and ios6UICollectionViewFlowLayout 和 ios6
【发布时间】:2014-05-05 11:41:47
【问题描述】:

我有一个扩展UICollectionFlowLayout。这通过将 attribute.frame 平移所需的数量并移动集合视图的可见 Rect 以显示转换后的单元格,从而使 UIcollectionViewCell 垂直居中。

这在 ios7 中运行良好。但是在 ios6 中,集合视图的可见 Rect 不会改变,因此第四个单元格显示为移位但被剪裁。

例如:-(NSArray*)layoutAttributesForElementsInRect:(CGRect)rect rect = (0,0,320,500) 我将单元格移动 200,然后开始显示从 (0,0,320,200) 到 (0,0,320,500) 的单元格,低于 500 的单元格将被剪裁。当它在 iOS7 中完美运行时,为什么会在 ios6 中发生这种情况?

@implementation VerticallyCenteredFlowLayout

    -(id)init
    {
        if (!(self = [super init])) return nil;
        [self setMinimumLineSpacing:5.0];
        [self setMinimumInteritemSpacing:0.0];
        [self setItemSize:CGSizeMake(10, 10)];
        [self setSectionInset:UIEdgeInsetsMake(0, 11, 11, 11)];
        [self setScrollDirection:UICollectionViewScrollDirectionVertical];
        return self;
    }

    -(NSArray*)layoutAttributesForElementsInRect:(CGRect)rect {
        NSArray* array = [super layoutAttributesForElementsInRect:rect];

        UICollectionViewLayoutAttributes* att = [array lastObject];
        if (att){
            CGFloat lastY = att.frame.origin.y + att.frame.size.height;
            CGFloat diff = self.collectionView.frame.size.height - lastY;
            if (diff > 0){
                for (UICollectionViewLayoutAttributes* a in array){
                    a.frame = CGRectMake(a.frame.origin.x, a.frame.origin.y + diff/2, a.frame.size.width, a.frame.size.height) ;
                }
            }
        }
        return array;
    }

【问题讨论】:

    标签: ios objective-c uicollectionviewcell uicollectionviewlayout


    【解决方案1】:

    ios6 中没有自动调整 contentSize。

    VerticallyCenteredFlowLayout 类中重写以下方法修复了问题

    -(CGSize)collectionViewContentSize {
        CGSize size = [super collectionViewContentSize];
        if (size.height < MIN(_maxHeight,self.collectionView.frame.size.height)) {
            size.height = MIN(_maxHeight,self.collectionView.frame.size.height);
        }
        return size;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-08-16
      • 2012-09-12
      • 1970-01-01
      • 1970-01-01
      • 2018-01-03
      • 2015-03-21
      • 1970-01-01
      相关资源
      最近更新 更多