Swift 中的 UIButton 图像即使在创建函数后也不会改变颜色

UIButton image in Swift does not change colors even after a function is created

我正在使用 Xcode 8 和 Swift 3,我正在尝试创建一个按钮,当我点击它时它会改变颜色。

按钮是黑色的,但当我点击时应该变成蓝色。该按钮是存钱罐的图像。

我继续添加黑猪和蓝猪的图像,分别命名为 money_offmoney_on

我现在正在编写代码以在单击时将黑色图像变为蓝色。代码本身没有显示错误,但模拟不成功。单击黑色存钱罐没有任何反应。

有没有人知道可能会发生什么?

import UIKit

class SecondViewController: UIViewController, UITextFieldDelegate {

    @IBOutlet weak var label: UILabel!
     let step: Float = 1

    @IBOutlet weak var moneybutton: UIButton!

    @IBOutlet var itemTextField: UITextField!

    @IBAction func moneyClicked(_ sender: UIButton) {
        if sender.currentImage === #imageLiteral(resourceName: "money_off"){
            sender.setImage(#imageLiteral(resourceName: "money_on"), for: .normal)}
        else {
            sender.setImage(#imageLiteral(resourceName: "money_off"), for: .normal)
        }
    }
}

您最好不要依赖某些 #imageLiteral 的 return 值,而是保持状态,例如下面示例代码中的 moneyOff

(抱歉,没有可用的 Xcode,因此代码可能包含一些语法错误,但您会明白的):

class SecondViewController: UIViewController, UITextFieldDelegate {

    @IBOutlet weak var label: UILabel!
    let step: Float = 1
    var moneyOff = true

    @IBOutlet weak var moneybutton: UIButton!

    @IBOutlet var itemTextField: UITextField!


    @IBAction func moneyClicked(_ sender: UIButton) {
        if moneyOff {
            sender.setImage(#imageLiteral(resourceName: "money_on"), for: .normal)}
        else {
            sender.setImage(#imageLiteral(resourceName: "money_off"), for: .normal)
        }
        moneyOff = !moneyOff // toggle state
    }
}

下面的代码适合我:

class ViewController: UIViewController {

     @IBOutlet weak var moneybutton: UIButton!
     func viewDidLoad() 
     {
       moneybutton.setImage(UIImage(named:"money_on"), for: .selected)
       moneybutton.setImage(UIImage(named:"money_off"), for: .normal)
     }

     @IBAction func moneyClicked(_ sender: UIButton)
     {
       if self.btn.isSelected
       {
         self.moneybutton.isSelected = false
       }
       else{
         self.moneybutton.isSelected = true
     }
 }

我想问题是你在 storyboardview controller 之间的连接。

我尝试以编程方式添加按钮并且成功了。

import UIKit
import AVFoundation

class ViewController: UIViewController {

    lazy var btn: UIButton = {
        let btn = UIButton()
        btn.translatesAutoresizingMaskIntoConstraints = false
        btn.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(handleClicked)))
        btn.isUserInteractionEnabled = true
        btn.setImage(UIImage(named: "money_off"), for: .normal)

        return btn
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .green

        view.addSubview(btn)

        btn.topAnchor.constraint(equalTo: view.topAnchor, constant: 100).isActive = true
        btn.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 50).isActive = true
        btn.widthAnchor.constraint(equalToConstant: 50).isActive = true
        btn.heightAnchor.constraint(equalToConstant: 10).isActive = true

    }

    func handleClicked() {
        if btn.currentImage == UIImage(named: "money_off")  {
            btn.setImage(UIImage(named: "money_on"), for: .normal)
        } else {
            btn.setImage(UIImage(named: "money_off"), for: .normal)
        }
    }
}

试试这个-

class SecondViewController: UIViewController, UITextFieldDelegate {
  @IBOutlet weak var moneybutton: UIButton!

  func viewDidLoad() {
    self.moneybutton.setImage(UIImage(named:"money_on"), for: .selected)
    self.moneybutton.setImage(UIImage(named:"money_off"), for: .normal)
  }

  @IBAction func moneyClicked(_ sender: UIButton) {
   self.moneybutton.isSelected = !self.moneybutton.isSelected
  }
}