循环创建按钮

十年热恋 提交于 2020-02-16 09:03:03

   CGFloat margin = 22;

    CGFloat btnW = 35;

    CGFloat btnH = 35;

    int cloums = 7;

    CGFloat btnLeft = (self.view.frame.size.width - (btnW * cloums) - (cloums - 1) * margin) / 2;

 

    for (int i = 0; i < 30; i++) {

        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

        

        // 行的索引

        int colIdx = i % cloums;

        // 列的索引

        int rowIdx = i / cloums;

        CGFloat btnX = btnLeft + colIdx * (btnW + margin);

        CGFloat btnY = 0 + rowIdx * (btnH + margin);

        button.frame = CGRectMake(btnX, btnY, btnW, btnH);

        button.backgroundColor = [UIColor redColor];

        [self.view addSubview:button];

    }

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!