gpt4 book ai didi

swift - 创建一个循环,每次调用一个值时加 40

转载 作者:行者123 更新时间:2023-11-30 10:33:04 25 4
gpt4 key购买 nike

我正在定位 3 个具有相同高度、宽度和相同 x 值的 uibtton,我唯一想要更改的是我希望每个 uibutton 添加 40 个正数以将其分开。我认为有某种方法可以通过循环来完成此操作,而不是像我下面那样手动执行此操作。我希望 (equalTo: view.centerYAnchor, Constant : 0) 在每次调用时都替换为 yP 加 40 。

   var yP = 0


undoButton.trailingAnchor.constraint(equalTo: view.centerXAnchor, constant :25),
undoButton.topAnchor.constraint(equalTo: view.centerYAnchor, constant : 0),
undoButton.widthAnchor.constraint(equalToConstant: CGFloat(widthBox)),
undoButton.heightAnchor.constraint(equalToConstant: 20),

clearButton.trailingAnchor.constraint(equalTo: view.centerXAnchor, constant :25),
clearButton.topAnchor.constraint(equalTo: view.centerYAnchor, constant : 40),
clearButton.widthAnchor.constraint(equalToConstant: CGFloat(widthBox)),
clearButton.heightAnchor.constraint(equalToConstant: 20),

color.trailingAnchor.constraint(equalTo: view.centerXAnchor, constant :25),
color.topAnchor.constraint(equalTo: view.centerYAnchor, constant : 80),
color.widthAnchor.constraint(equalToConstant: CGFloat(widthBox)),
color.heightAnchor.constraint(equalToConstant: 20),

最佳答案

试试这个。

func setConstraints() {
var yPosition: CGFloat = 0

[undoButton, clearButton, color].forEach { button in
NSLayoutConstraint.activate([
button.trailingAnchor.constraint(equalTo: view.centerXAnchor, constant :25),
button.topAnchor.constraint(equalTo: view.centerYAnchor, constant : yPosition),
button.widthAnchor.constraint(equalToConstant: CGFloat(widthBox)),
button.heightAnchor.constraint(equalToConstant: 20)
])
yPosition += 40
}
}

关于swift - 创建一个循环,每次调用一个值时加 40,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58684059/

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