gpt4 book ai didi

ios - 以编程方式循环 UIButton

转载 作者:行者123 更新时间:2023-11-30 12:23:09 25 4
gpt4 key购买 nike

我正在创建按钮,我想循环 14 次,2 行,每行有 7 个按钮,每个按钮之间有一点空间,我该怎么做?

这是我的代码:

var tileButton = UIButton()
func createButton() {

for i in 0...13 {

tileButton.frame = CGRect(x: 20, y: 530, width: 50, height: 50)
tileButton.layer.masksToBounds = true
tileButton.layer.cornerRadius = 15
tileButton.setImage( UIImage.init(named: "cell"), for: .normal)
tileButton.addTarget(self, action: "tileButton", for: UIControlEvents.touchUpInside)
tileButton.setTitle("\(String(i))", for: .normal)
view.addSubview(tileButton)

}
}

当我运行此代码时,仅显示 1 个按钮!

最佳答案

let BUTTON_PADDING: CGFloat = 10
func createButton() {

for i in 0...13 {
let y: CGFloat = i < 7 ? 530 : 600

let tileButton = UIButton(frame: .zero)

tileButton.frame = CGRect(x: (50*i) + BUTTON_PADDING, y: y, width: 50, height: 50)
tileButton.layer.masksToBounds = true
tileButton.layer.cornerRadius = 15
tileButton.setImage( UIImage.init(named: "cell"), for: .normal)
tileButton.addTarget(self, action: "tileButton", for: UIControlEvents.touchUpInside)
tileButton.setTitle("\(String(i))", for: .normal)
view.addSubview(tileButton)

}
}

关于ios - 以编程方式循环 UIButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44544923/

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