【问题标题】:How to get a button image to change to a different image when selected programmatically?以编程方式选择时如何让按钮图像更改为不同的图像?
【发布时间】:2022-02-02 10:45:11
【问题描述】:

当数组中的不同项目显示在标签中时,我有一个按钮可以更改图像。我希望按钮图像在选择按钮时更改为不同的图像。如何以编程方式执行此操作?我尝试了 button1.setImage(UIImage(named: "GreenHeart"), for: .selected),但这似乎不起作用(图像只是停留在 Heart)。出于某种原因,如果我使用 .highlighted 而不是 .selected 一切正常(我可以看到 GreenHeart 图像)。不确定这是否有帮助,但我是编码新手,所以我想我会在这里添加它以获得更多支持。

import UIKit
class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
}

@IBOutlet weak var quotesLabel: UILabel!
var firstQuote = -1
var quotes = ["The best time to plant a tree was 20 years ago - The next best time is today - Unknown",
                            "Everytime you spend money, you're casting a vote for the type of world you want - Anna Lappe",
                            "Buy less, choose well, make it last - Vivienne Westwood",
                            "The future depends on what we do in the present - Mahatma Gandhi",
]


@IBAction func nextButton(_ sender: Any) {
   
    if firstQuote < quotes.count{
    firstQuote = (firstQuote + 1) % quotes.count
    quotesLabel.text = quotes[firstQuote]
    }
    
    let quote1 = "The best time to plant a tree was 20 years ago - The next best time is today - Unknown"
    let quote2 = "Everytime you spend money, you're casting a vote for the type of world you want - Anna Lappe"
  
    let quote = quotesLabel.text

        let view1 = UIView()
        view.addSubview(view1)
        view1.frame = CGRect(x: 100, y: 500, width: 200, height: 100)
        view1.backgroundColor = .clear
        let button1 = UIButton(type: .custom)
        button1.setImage(UIImage(named: "Heart"), for: .normal)
        button1.setImage(UIImage(named: "GreenHeart"), for: .selected)
        
        view1.addSubview(button1)
        button1.frame = CGRect(x: 0, y: 0, width: 50, height: 50)
        button1.isHidden = true
    
        if quote == quote1{
                button1.isHidden = false
            } else {
                button1.isHidden = true
            }
    
        let button2 = UIButton(type: .custom)
        button2.setImage(UIImage(named: "GreenHeart"), for: .normal)
        view1.addSubview(button2)
        button2.frame = CGRect(x: 0, y: 0, width: 50, height: 50)
        button2.isHidden = true

        if quote == quote2{
            button2.isHidden = false
        } else {
            button2.isHidden = true
        }
}


}

【问题讨论】:

  • "我希望按钮图像在选择按钮时更改为不同的图像。"选择按钮 的代码部分在哪里?

标签: swift image button selected


【解决方案1】:

第一个问题是每次单击按钮时都会创建一个新视图和按钮。您只需要创建一次并执行此操作,您需要将该部分代码移动到 viewDidLoad() 中。

之后,如果您想在每次点击不同的按钮时更改按钮的图像,您可以创建一个变量:

var isSelected: Bool = false

在 nextButton() 方法中,切换布尔值。

isSelected = !isSelected.

这意味着,如果 isSelected 为真,它将为假。如果是假的,那就是真的。

然后,您可以检查布尔值是否为真,并将图像更改为布尔值。

button1.setImage(UIImage(name: isSelected ? "Heart": "GreenHeart"), for: .normal)

如果 isSelected 为 true,则显示 Heart 图片,如果为 false,则显示 GreenHeart。

@IBOutlet weak var quotesLabel: UILabel!

private let button1 = UIButton(type: .custom)
private var isSelected: Bool = false

var firstQuote = -1
var quotes = ["The best time to plant a tree was 20 years ago - The next best time is today - Unknown",
              "Everytime you spend money, you're casting a vote for the type of world you want - Anna Lappe",
              "Buy less, choose well, make it last - Vivienne Westwood",
              "The future depends on what we do in the present - Mahatma Gandhi",
]
override func viewDidLoad() {
    super.viewDidLoad()
    setupUI()
}

private func setupUI() {
    let view1 = UIView()
    view.addSubview(view1)
    view1.frame = CGRect(x: 100, y: 500, width: 200, height: 100)
    view1.backgroundColor = .clear
    
    view1.addSubview(button1)
    button1.frame = CGRect(x: 0, y: 0, width: 50, height: 50)
    
    // button1.isHidden = true
}

@IBAction func nextButton(_ sender: UIButton) {
    
    if firstQuote < quotes.count{
        firstQuote = (firstQuote + 1) % quotes.count
        quotesLabel.text = quotes[firstQuote]
        }
   
    isSelected = !isSelected

    let quote1 = "The best time to plant a tree was 20 years ago - The next best time is today - Unknown"
    let quote2 = "Everytime you spend money, you're casting a vote for the type of world you want - Anna Lappe"
    let quote = quotesLabel.text

    button1.setImage(UIImage(name: isSelected ? "Heart": "GreenHeart"), for: .normal)
    /*
    if quote == quote2{
        button1.isHidden = false
    } else {
        button1.isHidden = true
    } */
}

【讨论】:

  • 谢谢你。但是,这对我不起作用。我不得不将“UIImage(name:”)更改为 UIImage(named:),但这没有帮助。我可以看到按钮实际上是在单击,但图像没有改变。奇怪的是,如果我更改 private var isSelected: Bool = false to private var isSelected: Bool = true 它显示 GreenHeart 图像。这不是我想要它做的,但对我来说,我认为这表明它可以识别何时应该显示不同的图像。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-11
  • 2019-06-11
相关资源
最近更新 更多