如何动态更改自定义UIButton的图像

时间:2018-12-11 18:32:01

标签: ios swift uibutton

我创建了一个自定义UIButton类,使其看起来像下拉选择。我在按钮右侧有一个小的向下箭头图像。我想根据各种条件将按钮的图像更改为不同的图像,例如灰色,白色或红色。怎么做?以下是我的代码:

class DropDownButton: UIButton {

   let dropDownImageGrey = UIImage(named: "Icons/DropDown/Grey")
   let dropDownImageWhite = UIImage(named: "Icons/DropDown/White")
   let dropDownImageRed = UIImage(named: "Icons/DropDown/Red")

    override init(frame: CGRect) {
        super.init(frame: frame)
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    override func awakeFromNib() {
        super.awakeFromNib()

        self.imageEdgeInsets = UIEdgeInsets(top: 0, left: self.frame.width - 108, bottom: 0, right:0)
        self.setImage(dropDownImageGrey, for: [])
        self.titleEdgeInsets = UIEdgeInsets(top: 0, left: -13, bottom: 0, right:0)
    }
}

override func viewDidLoad() {
    // Change image to red one
    dropDownButton.??? // How to change?
}

3 个答案:

答案 0 :(得分:2)

您可能要使用e.IsHandled的{​​{1}}属性

setImage

如果图像是在UIButton中添加的,则可以使用override func viewDidLoad() { // Change image to red one let dropDownButton = DropDownButton() dropDownButton.setImage(dropDownButton.dropDownImageRed, for: .normal) } 或直接使用类似的名称,

Assets.xcassets

答案 1 :(得分:1)

我认为上述工作有一些答案。但是,出于生产代码的考虑,我建议对图像列表使用枚举。

class DropDownButton: UIButton {

    enum ImageType: String {
      case grey = "Icons/DropDown/Grey"
      case white = "Icons/DropDown/White"
      case red = "Icons/DropDown/Red"

      var image: UIImage? { return UIImage(named: rawValue) }
    }

    var imageType: ImageType = .red {
      didSet {
        setImage(imageType.image, for: .normal)
      }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    override func awakeFromNib() {
        super.awakeFromNib()

        self.imageEdgeInsets = UIEdgeInsets(top: 0, left: self.frame.width - 108, bottom: 0, right:0)
        self.titleEdgeInsets = UIEdgeInsets(top: 0, left: -13, bottom: 0, right:0)
    }
}

override func viewDidLoad() {
    // Change image to red one
    dropDownButton.imageType = .red
}

稍后,如果需要更改图像类型,只需设置按钮的imageType即可。喜欢

dropDownButton.imageType = .grey
dropDownButton.imageType = .white

答案 2 :(得分:0)

您可以使用枚举定义各种条件,并添加获取图像的方法。根据条件可以设置图像。示例如下:

在课堂外定义这个枚举

enum DropdownCondition
{
    case condition1

    case condition2

    case condition3

    func getImage() -> UIImage? {
        switch self {
        case .condition1:
            return UIImage(named: "Icons/DropDown/Grey")
        case .condition1:
            return UIImage(named: "Icons/DropDown/White")
        case .condition1:
            return UIImage(named: "Icons/DropDown/Red")
        default:
            return nil
        }
    }
}

在viewDidLoad / init中或根据条件调用任何方法yourMethodWithSomeoCndition(.condition1)。

override func viewDidLoad() {
    // Change image to red one
    let dropDownButton = DropDownButton()

    //Call based on your condition
    yourMethodWithSomeoCndition(.condition1)//This condition can change on the fly
}



func yourMethodWithSomeoCndition(_ condition:DropdownCondition)
{
    self.dropDownButton.setImage(condition.getImage(), for: .normal)
}
相关问题