gpt4 book ai didi

swift - 以编程方式将按钮放在一起

转载 作者:行者123 更新时间:2023-11-30 14:00:44 25 4
gpt4 key购买 nike

我以编程方式创建了一些按钮。这可以是 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)
})
}

最佳答案

试试这个:

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)) 以根据需要进行布局的示例。这里的关键是使用索引。

关于swift - 以编程方式将按钮放在一起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33040593/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com