【问题标题】:Button Color Changes when selection选择时按钮颜色变化
【发布时间】:2021-05-29 05:16:18
【问题描述】:

例如,我有两个按钮(按钮 1 和按钮 2)。

  • 按钮 1 已被选中,其背景颜色为红色,文本颜色为白色。
  • 按钮 2 背景颜色为灰色,文本颜色为红色。

如果我点击按钮 2,那么背景颜色将为红色,文本颜色为白色。

如果我点击按钮 1,它将变为灰色背景色和红色文本颜色。

如果我单击按钮 1,则背景颜色将为红色,文本颜色为白色,按钮 2 将变为灰色背景颜色和红色文本颜色。

那么如何以编程方式做到这一点?

@IBOutlet weak var button2: UIButton!
@IBOutlet weak var button1: UIButton!
    @IBAction func btnbutton2(_ sender: Any) {
        if (buttonTest2.isSelected == false){
        button2.backgroundColor = UIColor.gray
            button2.setTitleColor(UIColor.red, for: .normal)}
        if (button2.isSelected == true){
            button2.backgroundColor = UIColor.systemRed
            button2.setTitleColor(UIColor.white, for: .normal)}
    }
    @IBAction func btnbutton1(_ sender: Any) {
        if (button1.isSelected == true){
            button1.backgroundColor = UIColor.systemRed
            button1.setTitleColor(UIColor.white, for: .normal)}
        if (button1.isSelected == false){
        button1.backgroundColor = UIColor.gray
            button1.setTitleColor(UIColor.red, for: .normal)}
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        button1.backgroundColor = UIColor.systemRed
        button1.setTitleColor(UIColor.white, for: .normal)
        button2.backgroundColor = UIColor.gray
        button2.setTitleColor(UIColor.red, for: .normal)}

【问题讨论】:

  • 请出示您的代码,以展示您到目前为止所做的尝试。
  • 请在此处发布之前尝试搜索和阅读文档...button.backgroundColor = .red 和 button.setTitleColor(.white, for: [])
  • @creeperspeak 请检查更新后的问题以及我已完成的代码

标签: ios swift uibutton


【解决方案1】:

UIButton 的 Selected 属性仅影响标题颜色,前提是已为 Selected 状态设置了颜色。

.normal.highlighted和.selected可以设置标题颜色——背景颜色没有对应的状态。

此外,点击按钮不会改变其Selected 状态...您必须自己做。

试试这样:

class SelectButtonViewController: UIViewController {
    
    @IBOutlet var button1: UIButton!
    @IBOutlet var button2: UIButton!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        button1.isSelected = true

        // start button1 with "selected" state properties
        button1.backgroundColor = UIColor.systemRed
        button1.setTitleColor(UIColor.red, for: .normal)
        button1.setTitleColor(UIColor.lightGray, for: .highlighted)
        button1.setTitleColor(UIColor.white, for: .selected)

        // start button1 with "not selected" state properties
        button2.backgroundColor = UIColor.gray
        button2.setTitleColor(UIColor.red, for: .normal)
        button2.setTitleColor(UIColor.lightGray, for: .highlighted)
        button2.setTitleColor(UIColor.white, for: .selected)

    }
    
    @IBAction func btnbutton1(_ sender: Any) {
        button1.isSelected = true
        button1.backgroundColor = .systemRed
        button2.isSelected = false
        button2.backgroundColor = .gray
    }
    @IBAction func btnbutton2(_ sender: Any) {
        button2.isSelected = true
        button2.backgroundColor = .systemRed
        button1.isSelected = false
        button1.backgroundColor = .gray
    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-11-12
    • 1970-01-01
    • 2017-01-24
    • 1970-01-01
    • 2020-10-30
    • 1970-01-01
    相关资源
    最近更新 更多