gpt4 book ai didi

swift - UIButton 集合为最后按下的按钮设置颜色

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

我有一系列按钮,它们的作用类似于选择器菜单。我希望当我按下其中一个按钮时,它会更改其背景颜色,并且所有其他按钮都会返回到其初始颜色。

这就是我现在所拥有的

 @IBAction func optionSelected(_ sender: UIButton) {

for button in selectButtons {
if button.tag == sender.tag {

if !button.isSelected {
button.backgroundColor = palette.importantColorObject()
button.isSelected = true
} else {
button.backgroundColor = palette.clearGrayColorObject()
button.isSelected = false
}

}
}

但我不知道如何使只有最后一个选定的按钮具有此 importantColorObject 并且我还遇到一个问题,当我选择该按钮时,不仅其背景颜色发生变化,但看起来也像是内部文本被选中(蓝色)。我该如何解决这个问题?

提前致谢

最佳答案

这将解决您唯一的选择问题,这将确保您始终有一个选定的按钮,除非您取消选择选定的按钮并且所有按钮都变为灰色或未选定状态。

@IBAction func optionSelected(_ sender: UIButton) {

selectButtons.forEach { (button) in
button.backgroundColor = palette.clearGrayColorObject()
button.isSelected = false
}

sender.backgroundColor = palette.importantColorObject()
sender.isSelected = true
}

如果您也想要该功能,请使用这个

@IBAction func optionSelected(_ sender: UIButton) {
if sender.isSelected {
sender.backgroundColor = palette.clearGrayColorObject()
sender.isSelected = false
} else {
selectButtons.forEach { (button) in
button.backgroundColor = palette.clearGrayColorObject()
button.isSelected = false
}

sender.backgroundColor = palette.importantColorObject()
sender.isSelected = true
}
}

希望这会有所帮助。

关于swift - UIButton 集合为最后按下的按钮设置颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55211056/

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