美文网首页
UICollectionView居左显示 且自适应大小

UICollectionView居左显示 且自适应大小

作者: Aldon丶 | 来源:发表于2019-09-29 13:21 被阅读0次
    - (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){
                //第一列 避免被居中 如果有分区头视图  可注释该判断  添加判断是否为头视图
               //因为该layoutAttributes数组包含 头视图,如添加头视图,请在前面添加判断
                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有时间写一个上传

    相关文章

      网友评论

          本文标题:UICollectionView居左显示 且自适应大小

          本文链接:https://www.haomeiwen.com/subject/asvouctx.html