UICollectionView居左顯示 且自適應(yīng)大小

- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect{
    
    NSArray *array = [super layoutAttributesForElementsInRect:rect];
    for (int i = 0; i < array.count; i ++) {
        if (i != array.count - 1){
            UICollectionViewLayoutAttributes *currentAtt = array[i];
            UICollectionViewLayoutAttributes *nextAtt = array[i+1];
            if (CGRectGetMinY(currentAtt.frame) == CGRectGetMinY(nextAtt.frame)) {
                if ((CGRectGetMinX(nextAtt.frame) - CGRectGetMaxX(currentAtt.frame)) > self.minimumInteritemSpacing){
                    CGRect frame = nextAtt.frame;
                    CGFloat x = CGRectGetMaxX(currentAtt.frame) + self.minimumInteritemSpacing;
                    frame = CGRectMake(x, CGRectGetMinY(frame), frame.size.width, frame.size.height);
                    nextAtt.frame = frame;
                }
            }else{
                //下一列自動居左  避免被居中
                CGRect frame = nextAtt.frame;
                frame = CGRectMake(self.sectionInset.left, frame.origin.y, frame.size.width, frame.size.height);
                nextAtt.frame = frame;
            }
            if (i == 0){
                //第一個列 避免被居中
                CGRect frame = currentAtt.frame;
                frame = CGRectMake(self.sectionInset.left, frame.origin.y, frame.size.width, frame.size.height);
                currentAtt.frame = frame;
            }
        }
        else if (i == 0){
            //第一列 避免被居中 如果有分區(qū)頭視圖  可注釋該判斷  添加判斷是否為頭視圖
           //因為該layoutAttributes數(shù)組包含 頭視圖,如添加頭視圖,請在前面添加判斷
            UICollectionViewLayoutAttributes *currentAtt = array[i];
            CGRect frame = currentAtt.frame;
            frame = CGRectMake(self.sectionInset.left, frame.origin.y, frame.size.width, frame.size.height);
            currentAtt.frame = frame;
        }
    }
    return array;
}

cell里代碼使用的是masonry布局,不多做解釋

- (instancetype)initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame]){
        [self initViews];
    }
    return self;
}

- (void)initViews{
    self.contentView.backgroundColor = backColor;
    [self.contentView addSubview:self.desLabel];
    
    self.layer.cornerRadius = 15;
    self.layer.masksToBounds = true;
    
    [self.desLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.contentView);
        make.left.mas_equalTo(self.contentView).offset(10);
        make.right.mas_equalTo(self.contentView).offset(-10);
        make.width.lessThanOrEqualTo(@(kScreenWidth - 40));
        make.width.greaterThanOrEqualTo(@45);
    }];
}

- (UILabel *)desLabel{
    if (!_desLabel) {
        _desLabel = [[UILabel alloc]init];
        _desLabel.font = XTMFont(14);
        _desLabel.textColor = rgb(89, 89, 89);
        _desLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _desLabel;
}

collectionView里面estimatedItemSize隨便給個值就好

layout.estimatedItemSize = CGSizeMake(100, 30);

效果如圖:


151569551540_.pic.jpg

demo有時間寫一個上傳

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容