如何防止选定的UIButton在降落时更改为正常图像?

时间:2017-01-16 04:52:33

标签: ios uibutton

我有一张UIButton的两张图片。在其正常状态下,它是一支铅笔。当我选择它时,它是一篇论文。但是当我触及时,它显示了铅笔,即使我还没有选择它。我该如何防止这种情况?

@IBOutlet weak var btn: UIButton!

override func viewDidLoad() {
    super.viewDidLoad()
    btn.setImage(#imageLiteral(resourceName: "Pencil").withRenderingMode(.alwaysTemplate), for: .normal)
    btn.setImage(#imageLiteral(resourceName: "Paper").withRenderingMode(.alwaysTemplate), for: .selected)
    btn.tintColor = .cyan

    btn.addTarget(self, action: #selector(touchedButton), for: .touchUpInside)

}

func touchedButton() {
    if !btn.isSelected {
        btn.isSelected = true

    } else {
        btn.isSelected = false
        }



}

1 个答案:

答案 0 :(得分:0)

你可以使用UIButton的突出显示状态,例如,将图像设置为突出显示的状态,

btn.setImage(#imageLiteral(resourceName: 
            "Paper").withRenderingMode(.alwaysTemplate), for: .highlighted)