美文网首页
iOS使用Masonry 实现多宫格,并且自适应高度

iOS使用Masonry 实现多宫格,并且自适应高度

作者: 糖糖本尊 | 来源:发表于2018-08-08 11:30 被阅读372次

    废话不说先来张图,无图无真相 QQ20180808-111724-HD.gif

    代码实现如下:

    #import <UIKit/UIKit.h>
    
    @interface LLButttonsView : UIView
    ///本次需要添加宫格的个数
    @property(nonatomic ,assign)NSInteger buttonCount;
    ///传入宫格最大个数
    - (instancetype)initWithFrame:(CGRect)frame maxButtonsCount:(NSInteger)maxButtonsCount;
    @end
    
    #import "LLButttonsView.h"
    #import <Masonry/Masonry.h>
    @interface LLButttonsView()
    ///装多宫格的数组,复用视图
    @property(nonatomic ,strong)NSMutableArray <UIButton*>* buttons;
    ///容器视图 来自适应九宫格高度
    @property(nonatomic ,strong)UIView * containerView;
    ///容器视图的底部约束
    @property(nonatomic ,weak)MASConstraint * containerViewConstraintbottom;
    @end
    
    @implementation LLButttonsView
    
    -(void)setButtonCount:(NSInteger)buttonCount {
        _buttonCount = buttonCount;
    //每次重新创建宫格的个数 从容器视图中移除
        [self.containerView.subviews enumerateObjectsUsingBlock:^(UIButton * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if (!obj.hidden) {
                [obj removeFromSuperview];
                obj.hidden = true;
            }
        }];
     ///循环从初始化list中取出在添加到容器视图当中
        for (int i = 0; i< buttonCount; i++) {
            UIButton * btn = self.buttons[i];
             [self.containerView addSubview:btn];
            btn.hidden = false;
        }
        CGFloat gridWidth = 80;//格子的宽度
        CGFloat gridHeight = 30;//格子的高度
        NSInteger rowNumber = 3;//每行几个
        //间距x,y
        CGFloat marginX = ([UIScreen mainScreen].bounds.size.width - gridWidth * rowNumber) / (rowNumber + 1);
        CGFloat marginY = 20;
        
        for ( int  i = 0; i < self.containerView.subviews.count ; i++) {
            UIButton *subv = self.containerView.subviews[i];
            subv.backgroundColor = [UIColor colorWithHue:( arc4random() % 256 / 256.0 )
                                              saturation:( arc4random() % 128 / 256.0 ) + 0.5
                                              brightness:( arc4random() % 128 / 256.0 ) + 0.5
                                                   alpha:1];
            
            [subv mas_makeConstraints:^(MASConstraintMaker *make){
                make.left.mas_equalTo(marginX + i % rowNumber * (gridWidth + marginX));
                make.top.mas_equalTo(marginY + i / rowNumber * (gridHeight + marginY));
                make.width.mas_equalTo(gridWidth);
                make.height.mas_equalTo(gridHeight);
            }];
        }
    //卸载上一次容器视图的底部约束
        if (self.containerViewConstraintbottom) {
            [self.containerViewConstraintbottom uninstall];
        }
    //重新生成容器视图的底部约束 参考最后一个宫格的底部
        [self.containerView mas_makeConstraints:^(MASConstraintMaker *make) {
           self.containerViewConstraintbottom = make.bottom.equalTo(self.containerView.subviews.lastObject).offset(10);
        }];
        [self mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.containerView.mas_bottom);
        }];
    }
    - (instancetype)initWithFrame:(CGRect)frame maxButtonsCount:(NSInteger)maxButtonsCount
    {
        self = [super initWithFrame:frame];
        if (self) {
            self.backgroundColor = [UIColor redColor];
            for (int i = 0; i< maxButtonsCount; i++) {
                UIButton * button = [UIButton new];
                button.hidden = true;
                [self.buttons addObject:button];
            }
            [self addSubview:self.containerView];
            [self.containerView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.top.right.equalTo(self);
               //.conself.containerViewConstraintHeight =  make.height.mas_equalTo(0);
            }];
        }
        return self;
    }
    
    /// MARK: ---- 懒加载
    -(NSMutableArray *)buttons {
        if (!_buttons) {
            _buttons = [NSMutableArray array];
        }
        return _buttons;
    }
    -(UIView *)containerView {
        if (!_containerView) {
            _containerView = [UIView new];
        }
        return _containerView;
    }
    @end
    

    具体使用 如下:

     LLButttonsView * buttonView = [[LLButttonsView alloc] initWithFrame:CGRectZero maxButtonsCount:100];
        _buttonView = buttonView;
        [self.view addSubview:buttonView];
    //此处不用约束高度 ,已经在内部实现.只需要约束上下右即可
        [buttonView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.equalTo(self.view);
        }];
        buttonView.buttonCount = 9;
        
        UILabel * textLable = [UILabel new];
        textLable.numberOfLines = 0;
        [self.view addSubview:textLable];
        textLable.text = @"arc4random() 是一个真正的伪随机算法,不需要生成随机种子,因为第一次调用的时候就会自动生成。而且范围是rand()的两倍。在iPhone中,RAND_MAX是0x7fffffff (2147483647),而arc4random()返回的最大值则是 0x100000000 (4294967296)。作者:iOS_成才录链接:https://www.jianshu.com/p/f3f26608d1dd來源:简书简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。";
        textLable.textColor = [UIColor darkGrayColor];
        [textLable mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self.view);
            make.top.equalTo(buttonView.mas_bottom);
        }];
    # 模拟网络请求后调整宫格的个数
    -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
        
        self.buttonView.buttonCount = arc4random() % 10+1;
    }
    

    相关文章

      网友评论

          本文标题:iOS使用Masonry 实现多宫格,并且自适应高度

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