objective-c - 在另一个按钮下方添加按钮

标签 objective-c ios

我想添加一个在另一个之下的按钮。我有这个简单的代码:

- (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];
    }

}

我当然知道它会一个接一个地绘制。但是我可以在循环中在不知道高度的情况下执行此操作吗(因为高度取决于循环中的按钮数量)。

我想要实现的目标:

Buttons

最佳答案

试试这个:

- (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/

相关文章:

ios - 关闭图像选择器导致崩溃

ios - TensorFlow iOS 内存警告

ios - 当我移动触摸点时,我如何才能知道我是否有一个 UIView 长按?

ios - 如何获取文件夹和文件的列表 Google Drive API IOS?

c# - Xamarin.IOS 导入现有 Storyboard

objective-c - 类和 viewController.m 之间的委托(delegate)

ios - iOS 中的 iswalpha() 在 iOS 上返回的值与在 MacOS 上返回的值不同

ios - 使用 XMPP 在 iOS 中构建聊天应用程序

ios - Objective C UIViewController 子类?

objective-c - 清除UIImage使用的内存— UIImageView.image = nil不这样做