【问题标题】:Put buttons behind each other programatically以编程方式将按钮放在彼此后面
【发布时间】:2015-10-09 14:16:41
【问题描述】:

我以编程方式创建了一些按钮。这可以是 2/4 按钮之间的按钮。

我的问题是我如何以编程方式将它们设置在彼此之后,并确保无论数量多少(2/4)它们都垂直对齐?我就是这样做的

for button in optionButtons {
        button.frame = CGRectMake(220, 280, 150, 150) // x value should change?
        button.addTarget(self, action: "pressedOption:", forControlEvents: .TouchUpInside)
        dispatch_async(dispatch_get_main_queue(), {
            self.view.addSubview(button)
        })
 } 

【问题讨论】:

    标签: swift uibutton swift2


    【解决方案1】:

    试试这个:

    for (index, button) in enumerate(optionButtons) {
        button.frame = CGRectMake(220 * (index + 1), 280, 150, 150) // x value should change?
        button.addTarget(self, action: "pressedOption:", forControlEvents: .TouchUpInside)
        dispatch_async(dispatch_get_main_queue(), {
            self.view.addSubview(button)
        })
    }
    

    这是您必须修改 x 点逻辑 (220 * (index + 1)) 以根据需要进行布局的唯一示例。这里的关键是使用索引。

    【讨论】:

      猜你喜欢
      • 2010-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多