【问题标题】:UIButton selected image not workingUIButton 选择的图像不起作用
【发布时间】:2016-08-11 17:08:39
【问题描述】:

当 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选中时,不显示图片。

【问题讨论】:

  • 您检查了图像buttonChecked 是否存在且没有错字?
  • 您是否在视图中添加了radioButton?

标签: ios swift uibutton swift3


【解决方案1】:

您可以尝试添加这样的目标

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

在你改变图像的方法中:

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

【讨论】:

  • 是的,如果我不使用“选定”状态,这将是可能的。但我想使用它,因为按钮标签的默认彩色背景。还有一些想法? ;)
【解决方案2】:

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

    let radioButton = UIButton(type: .Custom)

而不是

    let radioButton = UIButton(type: .system)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-22
    • 1970-01-01
    • 1970-01-01
    • 2016-07-21
    • 2017-04-26
    • 1970-01-01
    • 2016-11-16
    • 1970-01-01
    相关资源
    最近更新 更多