iOS-Masonry九宫格布局终极版(for循环)

#pragma mark ========== Masonry九宫格布局 S=========================
    
    //九宫格的父视图
    UIView * jggContentView = [[UIView alloc]init];
    self.jggContentView = jggContentView;
    [self addSubview:jggContentView];
    [jggContentView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(imageBk.mas_bottom).mas_offset(18);
        make.left.right.mas_equalTo(0);
    }];
    
    NSArray * arrayTitle = @[@"我的房屋",@"物业报修",@"我的收藏",@"我的消息",@"我的管家",@"设置"];
    NSArray * arrayImage = @[@"我的房屋",@"物业报修",@"我的收藏",@"我的消息",@"我的管家",@"我的设置"];
    int SPNum = 3;//水平一行放几个
    CGFloat JGGMinX = 20;//起始x值
    CGFloat JGGMinY = 0;//起始y值
    CGFloat SPspace = 10;//水平距离
    CGFloat CXspace = 40;//垂直距离
    CGFloat widthJGG = (SCREEN_WIDTH- JGGMinX * 2 -SPspace * (SPNum-1)) / SPNum ;//九宫格宽
    CGFloat heightJGG = 80;//九宫格高

    for ( int  i = 0; i < arrayTitle.count ; i++) {
        //图片
        UIButton * buttonBig = [UIButton buttonWithType:UIButtonTypeCustom];
        buttonBig.imageView.contentMode = UIViewContentModeScaleAspectFill;
        buttonBig.clipsToBounds = YES;
        [buttonBig setImage:[UIImage imageNamed:arrayImage[i]] forState:UIControlStateNormal];
        [buttonBig setTitle:arrayTitle[i] forState:UIControlStateNormal];
        [buttonBig setTitleColor:MAIN_COLOR_333333 forState:UIControlStateNormal];
        buttonBig.titleLabel.font = [UIFont systemFontOfSize:14];
        [buttonBig addTarget:self action:@selector(ActionButtonBig:) forControlEvents:UIControlEventTouchUpInside];
        buttonBig.tag = 1001+i;
        [buttonBig setImagePositionWithType:SSImagePositionTypeTop spacing:10];
        [jggContentView addSubview:buttonBig];
        
        [buttonBig mas_makeConstraints:^(MASConstraintMaker *make){
            make.left.mas_equalTo(JGGMinX + i % SPNum * (widthJGG + SPspace));
            make.top.mas_equalTo(JGGMinY + i / SPNum * (heightJGG + CXspace));
            make.width.mas_equalTo(widthJGG);
            make.height.mas_equalTo(heightJGG);
            //不能再这里跟新约束,否则会警告,控件错位
        }];
        
#pragma mark ======= 最后跟新父视图约束 ====================
        if (i==arrayTitle.count-1) {
            //跟新约束
            [jggContentView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.bottom.mas_equalTo(buttonBig.mas_bottom).offset(0);
            }];
        }
    }

 #pragma mark ========== Masonry九宫格布局 E=========================

你可能感兴趣的:(iOS-Masonry九宫格布局终极版(for循环))