ios - 带图像的按钮,点击按钮时图像消失 iOS 10

标签 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 中单击按钮时按钮图像消失。

请帮我解决这个问题 谢谢提前

最佳答案

我认为这是使用 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/

相关文章:

ios - Textfield 确实结束了编辑崩溃应用程序

ios - 将 UIView 中心坐标从本地坐标转换为窗口基准坐标?

javascript - 我想要在 html 上显示某些类型图像的按钮

java - java可以在Android中做printIn吗?

ios - 数组中的随机数而不连续两次重复相同的数字?

ios - 未在 Objective-C 中调用自定义委托(delegate)方法

ios - 以编程方式使用自动布局将图像定位到 View 的中心

arrays - 使用 SwiftyJSON 反序列化时如何添加对象数组

swift/ cocoa : How to bind a value to currently selected tab

javascript - 如何创建一个脚本来单击behance页面上的所有关注按钮?