@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中的按钮时,按钮图像会消失。
请帮帮我 谢谢你提前
答案 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
}