美文网首页
iOS for循环创建九宫格

iOS for循环创建九宫格

作者: 隔墙送来秋千影 | 来源:发表于2018-05-17 15:46 被阅读100次

    for循环创建九宫格时,注意某个item是在第几行和第几列、
    上下间距自定义、

    - (void) setupButtonWithArr:(NSArray *)array {
        CGFloat btnW = (SCREEN_WIDTH - 40 *SCALE_WIDTH)/3;
        CGFloat btnH = btnW *1.35;
        for (int i = 0; i < array.count; i ++) {
            NSInteger row = i/3;
            NSInteger col = i%3;
            CGFloat btnX = 10 *SCALE_WIDTH + (btnW + 10 *SCALE_WIDTH) * col;
            CGFloat btnY = 10 *SCALE_WIDTH + (btnH + 50 *SCALE_WIDTH) * row;
            UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.frame = CGRectMake(btnX,btnY, btnW, btnH);
            [btn sd_setImageWithURL:[NSURL URLWithString:array[i][@"imgUrl"]] forState:UIControlStateNormal];
            [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
            btn.tag = i;
            [self.view addSubview:btn];
        }
    }
    

    相关文章

      网友评论

          本文标题:iOS for循环创建九宫格

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