我想使用循环在视图上生成按钮。 假设我想生成3个按钮。我在主视图中添加了一个uiview btnframe。现在我想使用该视图的坐标在该视图中添加按钮。我如何计算btnframe的界限?
答案 0 :(得分:3)
你想要按钮的尺寸是多少?如果将它们添加到视图中,它们会继承这些坐标,因此如果您执行0,0,它将位于您将其添加到视图的左上角。
for(int x=0;x<3;x++){
CGRect rect = CGRectMake(0,20 * x,100,20);
UIButton *button = [[UIButton alloc] initWithFrame:rect];
[btnframe addSubview:button];
.....
}
基础知识,这将为您提供三个按钮。
如果你想要一个这样的网格可以工作。
for(int x=0;x<5;x++){
for(int y=0;y<5;y++){
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(x * 100, y * 20, 100, 20)];
[button setText:[NSString stringWithFormat:@"%d,%d",x,y]];
[button addTarget:self action:@selector(changeView:) forControlEvents:UIControlEventTouchUpInside];
[mainView addSubview:button];
}
}
这会给你25个按钮,每行5个。