UIButton选择的图像不起作用

时间:2016-08-11 17:08:40

标签: ios swift uibutton swift3

我选择UIButton时遇到问题。代码位于 Swift 3

let radioButton = UIButton(type: .system)
radioButton.frame = CGRect(x: 0, y: 0, width: 40, height: 40)
radioButton.setImage(UIImage(named: "buttonUnchecked"), for: .normal)
radioButton.setImage(UIImage(named: "buttonChecked"), for: .selected)
radioButton.setTitle("This is some text.", for: .normal)
radioButton.sizeToFit()

radioButton.isSelected = true选择它时,不会显示图像。

2 个答案:

答案 0 :(得分:0)

你可以尝试添加像这样的目标

radioButton.addTarget(self, action: "addMethodName", forControlEvents: UIControlEvents.TouchUpInside)

并在方法中更改图像:

radioButton.setImage(UIImage(named: "buttonUnchecked"), for: .normal)

答案 1 :(得分:0)

您应该使用自定义按钮类型而不是系统类型。试试这个:

    let radioButton = UIButton(type: .Custom)

而不是

    let radioButton = UIButton(type: .system)