我有一个问题与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
选中时,不显示图像
您可以尝试添加这样一个目标
radioButton.addTarget(self, action: "addMethodName", forControlEvents: UIControlEvents.TouchUpInside)
,在方法中你像这样改变图像:
radioButton.setImage(UIImage(named: "buttonUnchecked"), for: .normal)
您可能应该使用自定义按钮类型而不是系统类型。试试这个:
let radioButton = UIButton(type: .Custom)
代替
let radioButton = UIButton(type: .system)