带图像的按钮,图像单击iOS 10按钮时消失

时间:2017-02-24 20:58:44

标签: ios swift button

@IBAction func btnPrimaryAction(_ sender: Any) {
        btnPrimary.setImage(UIImage(named : "checkbox_on.png") , for: UIControlState.normal)
        btnSecondary.setImage(UIImage(named : "checkbox_off.png"), for: UIControlState.normal)
        btnNewAdd.setImage(UIImage(named : "checkbox_off.png") , for: UIControlState.normal)
        payType = 1
    }

单击按钮时,我正在使用此代码更改按钮图像。但是,单击iOS 10中的按钮时,按钮图像会消失。

请帮帮我 谢谢你提前

1 个答案:

答案 0 :(得分:0)

我认为这是使用UIButton州的有效方式。 UIButton有多个状态,如UIControlState.normal,UIControlState.selected,UIControlState.highlighted等。 您可以使用UIButton UIControlState轻松解决您的问题。 首先,您需要像这样将Image设置为按钮。 在viewDidLoad方法。

// if you have add the "checkbox_off.png and checkbox_on.png" in Images.xcassets then get the image you need to write as below
//UIImage(named : "checkbox_off") no need to add extention like ".png"

btnPrimary.setImage(UIImage(named : "checkbox_off.png") , for: UIControlState.normal)
btnSecondary.setImage(UIImage(named : "checkbox_off.png"), for: UIControlState.normal)
btnNewAdd.setImage(UIImage(named : "checkbox_off.png") , for: UIControlState.normal)

// Selected    
btnPrimary.setImage(UIImage(named : "checkbox_on.png") , for: UIControlState.selected)
btnSecondary.setImage(UIImage(named : "checkbox_on.png"), for: UIControlState.selected)
btnNewAdd.setImage(UIImage(named : "checkbox_on.png") , for: UIControlState.selected)

现在在您的函数中只需更改按钮状态

@IBAction func btnPrimaryAction(_ sender: Any) {
    btnPrimary.selected = true
    btnSecondary.selected = false
    btnNewAdd.selected = false
    payType = 1
}