UICollectionView,实现自适应宽度和高度,并且设置cell对齐方式

UICollectionView,实现自适应宽度和高度,并且设置cell对齐方式,第1张

UICollectionView,实现自适应宽度和高度,并且设置cell对齐方式

参考:https://www.jianshu.com/p/de08c2679241
1 创建UICollectionView,实现代理

-(void)createView{
        //创建一个流水布局
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
        //设置cell的尺寸(宽度和高度)
        //设置竖直滚动放向(默认是竖直方向)
        layout.scrollDirection = UICollectionViewScrollDirectionVertical;
        //设置初始值,预估大小
        layout.estimatedItemSize =  CGSizeMake(200, 40);

        UICollectionView * MyCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        [self addSubview:MyCollectionView];
        MyCollectionView.delegate = self;
        MyCollectionView.dataSource = self;
        [MyCollectionView registerClass:[OnePicCollectionView class] forCellWithReuseIdentifier:@"UICollectionViewCell"];
        [MyCollectionView registerClass:[CollectionReusableView_OnePic class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"UICollectionElementKindSectionHeader"];
        [MyCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.bottom.mas_equalTo(0);
        }];
    //设置cell对齐方式
    SEL sel = NSSelectorFromString(@"_setRowAlignmentsOptions:");
    if ([MyCollectionView.collectionViewLayout respondsToSelector:sel]) {
        ((void(*)(id,SEL,NSDictionary*))objc_msgSend)(MyCollectionView.collectionViewLayout,sel,
                                                      @{@"UIFlowLayoutCommonRowHorizontalAlignmentKey":@(NSTextAlignmentLeft),
                                                        @"UIFlowLayoutLastRowHorizontalAlignmentKey" : @(NSTextAlignmentLeft),
                                                        @"UIFlowLayoutRowVerticalAlignmentKey" : @(NSTextAlignmentCenter)});
    }
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 3;
}
-(__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    OnePicCollectionView *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"UICollectionViewCell" forIndexPath:indexPath];
    cell.indexRow = indexPath.item;
    cell.backgroundColor = @[UIColor.grayColor,UIColor.yellowColor,UIColor.greenColor][indexPath.item%3];
    cell.backgroundColor = cell.contentView.backgroundColor = UIColor.yellowColor;
    cell.textLabel.text = @[@"kahdkasdhj01289081203812080122922kahdjkahs1",@"9127389127897123987129387123897112738912782212h97112738912782212h",@"22323"][indexPath.item];
    return cell;
}

2 创建自适应宽度高度UICollectionViewCell
OnePicCollectionView.h 文件

#import 

NS_ASSUME_NONNULL_BEGIN

@interface OnePicCollectionView : UICollectionViewCell

@property (nonatomic, strong) UILabel *textLabel;

@end

NS_ASSUME_NONNULL_END

OnePicCollectionView.m 文件

#import "OnePicCollectionView.h"

@implementation OnePicCollectionView
-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self setUI];
    }
    return self;
}
-(void)setUI{
    self.textLabel = [[UILabel alloc] init];
    [self.contentView addSubview:self.textLabel];
    self.textLabel.numberOfLines = 0;
    self.textLabel.adjustsFontSizeToFitWidth = YES;
    [self.textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(10);
        make.right.bottom.mas_equalTo(-10);
        make.width.mas_lessThanOrEqualTo(300);
    }];
    self.textLabel.font = [UIFont systemFontOfSize:14];
    self.textLabel.textColor = UIColor.blackColor;
    self.textLabel.textAlignment = NSTextAlignmentLeft;
    self.textLabel.backgroundColor = UIColor.blueColor;
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/997025.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)

保存