我想为键盘画出26个按钮。共有3行。第一行有10个按钮,第二行有9个,第三个有7个。从一个按钮到另一个按钮的空间是5.每个按钮的宽度为:27和高度:40。如何同时绘制它们?
答案 0 :(得分:1)
你可以像这样按钮: - 它不是你问题的解决方案但是......你仍然会从那个代码中得到这个想法.-
objDelegate.redColorArray = [[NSMutableArray alloc] initWithObjects:@"255",@"215",@"242",@"198",@"204",@"119",@"217",@"149",@"112",@"79",@"247",@"55",@"0",@"0",@"255",@"16", nil];
objDelegate.greenColorArray = [[NSMutableArray alloc] initWithObjects:@"255",@"228",@"220",@"217",@"193",@"147",@"150",@"179",@"48",@"98",@"150",@"96",@"0",@"176",@"0",@"37", nil];
objDelegate.blueColorArray = [[NSMutableArray alloc] initWithObjects:@"255",@"189",@"219",@"241",@"218",@"60",@"148",@"215",@"160",@"40",@"70",@"146",@"0",@"80",@"0",@"63", nil];
UIButton *btn[16];
buttonArray = [[NSMutableArray alloc] init];
int x=0;
int y=0;
for (int i=0; i<16; i++)
{
btn[i] = [[UIButton alloc] initWithFrame:CGRectMake(x, y, 15, 15)];
[btn[i] setBackgroundColor:[UIColor colorWithRed:[[objDelegate.redColorArray objectAtIndex:i]floatValue]/255.0 green:[[objDelegate.greenColorArray objectAtIndex:i]floatValue]/255.0 blue:[[objDelegate.blueColorArray objectAtIndex:i]floatValue]/255.0 alpha:1]];
btn[i].tag = i;
[btn[i] addTarget:self action:@selector(setColor:) forControlEvents:UIControlEventTouchUpInside];
[buttonArray addObject:btn[i]];
[self addSubview:btn[i]];
x+=20;
if((i+1)%4==0)
{
x=0;
y+=20;
}
}
它会显示这样的按钮