How to center align the cells of a UICollectionView?

前端 未结 18 2495
梦如初夏
梦如初夏 2020-11-28 01:33

I am currently using UICollectionView for the user interface grid, and it works fine. However, I\'d like to be enable horizontal scrolling. The grid supports 8

18条回答
  •  伪装坚强ぢ
    2020-11-28 02:04

    This is how i obtained a collection view that was center aligned with a fixed Interitem spacing

    #define maxInteritemSpacing 6
    #define minLineSpacing 3
    
    @interface MyFlowLayout()
    
    @end
    
    @implementation MyFlowLayout
    
    - (instancetype)init
    {
        self = [super init];
        if (self) {
            self.minimumInteritemSpacing = 3;
            self.minimumLineSpacing = minLineSpacing;
            self.scrollDirection = UICollectionViewScrollDirectionVertical;
        }
        return self;
    }
    
    - (NSArray *) layoutAttributesForElementsInRect:(CGRect)rect {
        NSArray *answer = [super layoutAttributesForElementsInRect:rect];
    
        if (answer.count > 0) {
            NSMutableArray *> *rows = [NSMutableArray array];
            CGFloat maxY = -1.0f;
            NSInteger currentRow = 0;
    
            //add first item to row and set its maxY
            [rows addObject:[@[answer[0]] mutableCopy]];
            maxY = CGRectGetMaxY(((UICollectionViewLayoutAttributes *)answer[0]).frame);
    
            for(int i = 1; i < [answer count]; ++i) {
                //handle maximum spacing
                UICollectionViewLayoutAttributes *currentLayoutAttributes = answer[i];
                UICollectionViewLayoutAttributes *prevLayoutAttributes = answer[i - 1];
                NSInteger maximumSpacing = maxInteritemSpacing;
                NSInteger origin = CGRectGetMaxX(prevLayoutAttributes.frame);
    
                if(origin + maximumSpacing + currentLayoutAttributes.frame.size.width < self.collectionViewContentSize.width) {
                    CGRect frame = currentLayoutAttributes.frame;
                    frame.origin.x = origin + maximumSpacing;
                    currentLayoutAttributes.frame = frame;
                }
    
                //handle center align
                CGFloat currentY = currentLayoutAttributes.frame.origin.y;
                if (currentY >= maxY) {
                    [self shiftRowToCenterForCurrentRow:rows[currentRow]];
    
                    //next row
                    [rows addObject:[@[currentLayoutAttributes] mutableCopy]];
                    currentRow++;
                }
                else {
                    //same row
                    [rows[currentRow] addObject:currentLayoutAttributes];
                }
    
                maxY = MAX(CGRectGetMaxY(currentLayoutAttributes.frame), maxY);
            }
    
            //mark sure to shift 1 row items
            if (currentRow == 0) {
                [self shiftRowToCenterForCurrentRow:rows[currentRow]];
            }
        }
    
        return answer;
    }
    
    - (void)shiftRowToCenterForCurrentRow:(NSMutableArray *)currentRow
    {
        //shift row to center
        CGFloat endingX = CGRectGetMaxX(currentRow.lastObject.frame);
        CGFloat shiftX = (self.collectionViewContentSize.width - endingX) / 2.f;
        for (UICollectionViewLayoutAttributes *attr in currentRow) {
            CGRect shiftedFrame = attr.frame;
            shiftedFrame.origin.x += shiftX;
            attr.frame = shiftedFrame;
        }
    }
    
    @end
    

提交回复
热议问题