UICollectionView 實(shí)現(xiàn)等間距自適應(yīng)寬度cell

UICollectionViewCell內(nèi)的操作

1.撐起cell的lable自適應(yīng)

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        // 用約束來初始化控件:
        self.textLabel = [[UILabel alloc] init];
        self.textLabel.textAlignment =NSTextAlignmentCenter;
        [self.contentView addSubview:self.textLabel];
        
        [self.textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            // make 代表約束:
            make.top.equalTo(self.contentView).with.offset(0);   // 對當(dāng)前view的top進(jìn)行約束,距離參照view的上邊界是 :
            make.left.equalTo(self.contentView).with.offset(0);  // 對當(dāng)前view的left進(jìn)行約束,距離參照view的左邊界是 :
            make.height.equalTo(@(itemHeight));                // 高度
            make.right.equalTo(self.contentView).with.offset(0); // 對當(dāng)前view的right進(jìn)行約束,距離參照view的右邊界是 :
        }];
    }
    return self;
}

2.要重寫UICollectionViewLayoutAttributes方法

- (UICollectionViewLayoutAttributes *)preferredLayoutAttributesFittingAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes{
    UICollectionViewLayoutAttributes *attributes = [super preferredLayoutAttributesFittingAttributes:layoutAttributes];
    //32是cell與父視圖左右邊距
    CGRect rect = [self.textLabel.text boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width-32, itemHeight) options:NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesFontLeading|NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:     [UIFont systemFontOfSize:13]} context:nil];
    rect.size.width+=24;//lable與cell左右的間距
    rect.size.height+=14;//lable與cell上下的間距
    attributes.frame = rect;
    return attributes;
    
}

UICollectionView里的操作

1.創(chuàng)建一個(gè)UICollectionViewFlowLayout的子類重寫父類的方法- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect

- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect {
    NSMutableArray * attributes = [[super layoutAttributesForElementsInRect:rect] mutableCopy];
   // 設(shè)置的最大間距,一般這個(gè)間距是固定的,就用外部能寫的最小間距值
    CGFloat maximumSpacing = self.minimumInteritemSpacing;
    
    if (attributes.count > 0) {
        UICollectionViewLayoutAttributes *firstAttributes = attributes[0];
        CGRect frame = firstAttributes.frame;
        frame.origin.x = self.sectionInset.left;
        firstAttributes.frame = frame;
    }
    //第一個(gè)frame不用更改 從第二個(gè)循環(huán)到最后一個(gè)
    for (NSInteger i = 1 ; i < attributes.count ; i++ ) {
        // 當(dāng)前的attribute
        UICollectionViewLayoutAttributes * currentLayoutAttributes = attributes[i];
        // 上一個(gè)attribute
        UICollectionViewLayoutAttributes * prevLayoutAttributes = attributes[i - 1];
        // 前一個(gè)cell的最右邊
        CGFloat origin = CGRectGetMaxX(prevLayoutAttributes.frame);
        // 如果當(dāng)前一個(gè)cell的最右邊加上我們的想要的間距加上當(dāng)前cell的寬度依然在contentSize中,我們改變當(dāng)前cell的原點(diǎn)位置
        // 不加這個(gè)判斷的后果是,UICollectionView只顯示一行,原因是下面所有的cell的x值都被加到第一行最后一個(gè)元素的后面了 考慮cell與view的左右間距
        if (origin + maximumSpacing + currentLayoutAttributes.frame.size.width < self.collectionViewContentSize.width -self.sectionInset.right ) {
            CGRect frame = currentLayoutAttributes.frame;
            frame.origin.x = origin + maximumSpacing;
            currentLayoutAttributes.frame = frame;
        } else {
            CGRect frame = currentLayoutAttributes.frame;
            frame.origin.x = self.sectionInset.left;
            currentLayoutAttributes.frame = frame;
        }
    }
    return attributes;
}

estimatedItemSize設(shè)置

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

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

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