我想添加一个在另一个之下的按钮。我有这个简单的代码:
- (void)viewDidLoad
{
[super viewDidLoad];
for(int i=0 ; i<9 ; i++)
{
UIButton *myButton = [[UIButton alloc] init];
myButton.frame = CGRectMake(self.view.frame.origin.x, self.view.frame.origin.y, self.view.frame.size.width, self.view.frame.size.height/10); //this "10" i want also dynamically
myButton.backgroundColor = [UIColor blackColor];
[self.view addSubview:myButton];
}
}
我当然知道它会一个接一个地绘制。但是我可以在循环中在不知道高度的情况下执行此操作吗(因为高度取决于循环中的按钮数量)。
我想要实现的目标:
最佳答案
试试这个:
- (void)viewDidLoad
{
[super viewDidLoad];
int number = 10;
for(int i=0 ; i<9 ; i++)
{
UIButton *myButton = [[UIButton alloc] init];
myButton.frame = CGRectMake(self.view.frame.origin.x, (self.view.frame.size.height/number)*i + number, self.view.frame.size.width, self.view.frame.size.height/number);
myButton.backgroundColor = [UIColor blackColor];
[self.view addSubview:myButton];
}
}
关于objective-c - 在另一个按钮下方添加按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11608335/