【问题标题】:Swift UIButton Subclass and change color based on variableSwift UIButton 子类并根据变量更改颜色
【发布时间】:2018-11-29 06:22:24
【问题描述】:

我正在为我的 UIButton 使用一个子类,它有一个名为 isActive 的变量。我需要根据该变量更改按钮边框颜色。此变量将以编程方式更改。请帮我解决这个问题。

@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
    }
}
}

【问题讨论】:

  • 使用属性观察者 didSet、willSet。每当您更新属性值时,这些方法都会调用。看看吧nshipster.com/swift-property-observers
  • @hashHb get {return self.isActive} 将被递归调用。因此,请尝试{return isActive}。同样在set (active) 中,只有当active 为真时才会调用commonInit 方法。所以你可以试试set (active) { isActive = active;commonInit(isActive: active)}
  • 就我个人而言,我根本不建议使用这些属性观察器,因为调用者不清楚属性更改可能会产生什么副作用。我只是为它创建一个方法,这样调用者就更清楚事情可以改变了。

标签: ios swift uibutton subclass programmatically


【解决方案1】:

您应该观察didSet 以更新view。在Swift 中,类型名称应以大写开头,例如ButtonCTAOutlineDark。请看固定类,

@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
        }
    }
}

【讨论】:

  • 谢谢@Kamran。这有帮助。
【解决方案2】:

您的isActive 属性写入不正确。它首先不应该是计算属性。目前,getter 只会导致无限递归,而 setter 实际上并没有设置任何东西。

isActive 属性应该是带有didSet 属性观察者的存储属性:

@IBInspectable
var isActive: Bool {
    didSet {

    }
}

didSet里面,你可以只放commonInit的最后一部分。 commonInit 的第一部分不需要在每次isActive 更改时运行。我建议您将其提取为称为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
    }

}

然后在didSet 中,您可以直接调用它:

updateBorder(isActive: isActive)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-31
    • 2021-09-23
    • 2019-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多