美文网首页
九宫格布局

九宫格布局

作者: crazy一笑 | 来源:发表于2018-04-18 16:14 被阅读14次
    for (NSInteger i = 0; i < self.vipModels.count; i++) {
            JQVipModel *model = self.vipModels[i];
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            [btn setTitle:model.vipLevel forState:UIControlStateNormal];
            [btn setTitleColor:[Toolkit getColor:@"b2b2b2"] forState:UIControlStateNormal];
            btn.layer.borderColor = [Toolkit getColor:@"b2b2b2"].CGColor;
            btn.backgroundColor = [Toolkit getColor:@"f6f5f6"];
            btn.layer.borderWidth = 1. / [UIScreen mainScreen].scale;
            btn.titleLabel.adjustsFontSizeToFitWidth = YES;
            btn.tag = kBaseTag + i;
            
            //列
            NSInteger column = 2;
            //行,取天花板值
            NSInteger row = ceil(self.vipModels.count * 1.0 / column);
            CGFloat leftRightMargin = 30 * zxmScale;
            CGFloat topBottomMargin = 30 * zxmScale;
            CGFloat btnMargin = 20 * zxmScale;
            CGFloat btnW = (ScreenWidth_ZXM - leftRightMargin * 2 - btnMargin * (column - 1)) * (1./column);
            CGFloat btnH = 80 * zxmScale;
            
           //关键代码就这两句
            NSInteger rowIndex = i / column;
            NSInteger columnIndex = i % column;
            
            self.levelBtnViewH = (topBottomMargin + btnH) * row + 50 * zxmScale;
            
            btn.frame = CGRectMake(leftRightMargin + (btnW + btnMargin) * columnIndex, topBottomMargin + rowIndex * (btnH + topBottomMargin), btnW, btnH);
            
            [btn addTarget:self action:@selector(selectedBtnClick:) forControlEvents:UIControlEventTouchUpInside];
            
            [self.levelBtns addObject:btn];
            [self.levelBtnView addSubview:btn];
            
            if (i == 0) {
                [self selectedBtnClick:btn];
            }
    }
    

    相关文章

      网友评论

          本文标题:九宫格布局

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