@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 中单击按钮时按钮图像消失。
请帮我解决这个问题 谢谢提前
最佳答案
我认为这是使用 UIButton
的有效方法状态。 UIButton
有多个状态,如 UIControlState.normal,UIControlState.selected,UIControlState.highlighted
ETC..
您可以使用 UIButton 的 UIControlState
轻松解决您的问题。
首先,您需要像这样将图像设置为按钮。
在 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
}
关于ios - 带图像的按钮,点击按钮时图像消失 iOS 10,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42448165/