美文网首页
iOS开发,用for创建6个按钮,摆成两行三列,代码该怎么写?

iOS开发,用for创建6个按钮,摆成两行三列,代码该怎么写?

作者: 幻想另类美 | 来源:发表于2017-12-21 15:19 被阅读30次

define Start_X 10.0f // 第一个按钮的X坐标

define Start_Y 50.0f // 第一个按钮的Y坐标

define Width_Space 5.0f // 2个按钮之间的横间距

define Height_Space 20.0f // 竖间距

define Button_Height 122.0f // 高

define Button_Width 75.0f // 宽

-(void)addButtonS
{
for (int i = 0 ; i < 6; i++) {
NSInteger index = i % 3;
NSInteger page = i / 3;

    // 圆角按钮
    UIButton *aBt = [UIButton buttonWithType:UIButtonTypeRoundedRect];
     
    aBt.frame = CGRectMake(index * (Button_Width + Width_Space) + Start_X, page  * (Button_Height + Height_Space)+Start_Y, Button_Width, Button_Height);
     
    [self.view addSubview:aBt];
}

}

相关文章

网友评论

      本文标题:iOS开发,用for创建6个按钮,摆成两行三列,代码该怎么写?

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