简体   繁体   English

Swift UIButton子类并根据变量更改颜色

[英]Swift UIButton Subclass and change color based on variable

I'm using a subclass for my UIButton and it has a variable called isActive. 我正在为我的UIButton使用一个子类,它有一个名为isActive的变量。 I need to change the button border color based on that variable. 我需要根据该变量更改按钮边框颜色。 This variable will change programmatically. 此变量将以编程方式更改。 Please help me with this. 请帮我解决一下这个。

@IBDesignable
class buttonCTAOutlineDark: UIButton {

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

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

override func prepareForInterfaceBuilder() {
    commonInit()
}

@IBInspectable var isActive: Bool {
    get {
        return self.isActive
    }
    set (active) {
        if active {
            commonInit(isActive: active)
        }
    }
}

func commonInit(isActive: Bool = false) {
    self.backgroundColor = .clear
    self.layer.cornerRadius = 4
    self.layer.borderWidth = 1

    if (isActive) {
        self.tintColor = ACTIVE_COLOR
        self.layer.borderColor = ACTIVE_COLOR.cgColor
    } else {
        self.tintColor = nil
        self.layer.borderColor = UIColor(red:0.69, green:0.72, blue:0.77, alpha:1.0).cgColor
    }
}
}

You should be observing didSet to update the view . 您应该观察didSet以更新view In Swift , type names should start with Uppercase eg ButtonCTAOutlineDark . Swift ,类型名称应以大写ButtonCTAOutlineDark开头,例如ButtonCTAOutlineDark Please see the fixed class, 请看固定班级,

@IBDesignable
class ButtonCTAOutlineDark: UIButton {

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

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

    @IBInspectable var isActive: Bool = false {
        didSet {
            self.commonInit(isActive: self.isActive)
        }
    }

    func commonInit(isActive: Bool = false) {
        self.backgroundColor = .clear
        self.layer.cornerRadius = 4
        self.layer.borderWidth = 1

        if (isActive) {
            self.tintColor = ACTIVE_COLOR
            self.layer.borderColor = ACTIVE_COLOR.cgColor
        } else {
            self.tintColor = nil
            self.layer.borderColor = UIColor(red:0.69, green:0.72, blue:0.77, alpha:1.0).cgColor
        }
    }
}

Your isActive property is written incorrectly. 您的isActive属性写得不正确。 It should not be a computed property in the first place. 它首先不应该是计算属性。 Currently, the getter will just cause infinite recursion and the setter doesn't actually set anything. 目前,getter只会导致无限递归,而setter实际上并没有设置任何东西。

The isActive property should be a stored property with a didSet property observer: isActive属性应该是带有didSet属性observer的存储属性:

@IBInspectable
var isActive: Bool {
    didSet {

    }
}

Inside didSet , you can just put the last part of commonInit . didSet ,你可以放下commonInit的最后一部分。 The first part of commonInit doesn't need to be run every time isActive changes. 每次isActive更改时都不需要运行commonInit的第一部分。 I recommend you to extract that as a method called updateBorder : 我建议你将其解压缩为一个名为updateBorder的方法:

func updateBorder(isActive: Bool) {

    if (isActive) {
        self.tintColor = ACTIVE_COLOR
        self.layer.borderColor = ACTIVE_COLOR.cgColor
    } else {
        self.tintColor = nil
        self.layer.borderColor = UIColor(red:0.69, green:0.72, blue:0.77, alpha:1.0).cgColor
    }

}

And then in didSet , you can just call that: 然后在didSet ,您可以调用它:

updateBorder(isActive: isActive)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM