Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios Swift UIButton子类并根据变量更改颜色_Ios_Swift_Uibutton_Subclass_Programmatically - Fatal编程技术网

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

Ios Swift UIButton子类并根据变量更改颜色,ios,swift,uibutton,subclass,programmatically,Ios,Swift,Uibutton,Subclass,Programmatically,我正在为UIButton使用一个子类,它有一个名为isActive的变量。我需要根据该变量更改按钮边框颜色。此变量将以编程方式更改。请帮我做这个 @IBDesignable class buttonCTAOutlineDark: UIButton { override init(frame: CGRect) { super.init(frame: frame) commonInit() } required init?(coder aDecoder: NSCoder) {

我正在为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
以更新
视图。在
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
        }
    }
}

您的
isActive
属性写入错误。它首先不应该是计算属性。目前,getter只会导致无限递归,setter实际上没有设置任何内容

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

@IBInspectable
var isActive: Bool {
    didSet {

    }
}
didSet
中,您只需放置
commonInit的最后一部分即可。
commonInit
的第一部分不需要每次
isActive
更改时都运行。我建议您将其提取为名为
updateorder
的方法:

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)

使用属性observer didSet、willSet。无论何时更新属性值,这些方法都将调用。看看它@hashb
get{return self.isActive}
将被递归调用。因此,请尝试
{return isActive}
。同样在
set(active)
中,只有当
active
为true时才会调用
commonInit
方法。因此,您可以尝试
set(active){isActive=active;commonInit(isActive:active)}
我个人不建议使用这些属性观察器,因为调用者不清楚属性更改会产生什么副作用。我只想为它创建一个方法,这样,调用者就更清楚事情是可以改变的。谢谢@Kamran。这有帮助。