9

我正在尝试实现一个简单的 IBDesignable UIButton 子类。我希望能够从 Interface Builder 为控件的每个状态设置颜色。我知道这可以通过 IBInspectable 关键字来实现。在 state 属性上使用 KVO 时,我遇到了 IB 崩溃的问题。IBDesignable 调试器在 deinit 上崩溃。有谁知道我如何与 KVO 和 IBDesignable 一起工作?

@IBDesignable
class UIButtonActionButton: UIButton {

    @IBInspectable var defaultColour: UIColor = UIColor.blueColor() {
        didSet {
            self.setNeedsDisplay()
        } 
    }

    @IBInspectable var selectedColour: UIColor = UIColor.blueColor()

    @IBInspectable var disabledColour: UIColor = UIColor.grayColor()

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

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


    private func _setup(){
        self.addObserver(self, forKeyPath: "state", options: NSKeyValueObservingOptions.New, context: nil)
        self.layer.cornerRadius = 5.0
        self.layer.masksToBounds = true
    }

    override func observeValueForKeyPath(keyPath: String, ofObject object: AnyObject, change: [NSObject : AnyObject], context: UnsafeMutablePointer<Void>) {
        self.setNeedsDisplay()
    }

    override func drawRect(rect: CGRect) {
        super.drawRect(rect)
        let context = UIGraphicsGetCurrentContext()

        if self.highlighted {
            CGContextSetFillColorWithColor(context, selectedColour.CGColor)
            CGContextFillRect(context, self.bounds)
        } else if self.state == UIControlState.Disabled {
            CGContextSetFillColorWithColor(context, disabledColour.CGColor)
            CGContextFillRect(context, self.bounds)
        } else {
            CGContextSetFillColorWithColor(context, defaultColour.CGColor)
            CGContextFillRect(context, self.bounds)
        }
    }

    deinit {
        self.removeObserver(self, forKeyPath: "state", context: nil)
    }

}
4

2 回答 2

9

对于Xcode 7.2的通用代码@IBDesignable UIButton Subclass如下所示:

import UIKit

@IBDesignable class MyButton: UIButton {

    //this init fires usually called, when storyboards UI objects created:
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        self.setupViews()
    }

    //This method is called during programmatic initialisation
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupViews()
    }


    func setupViews() {
        //your common setup goes here
    }

    //required method to present changes in IB
    override func prepareForInterfaceBuilder() {
        super.prepareForInterfaceBuilder()
        self.setupViews()
    }
}
于 2016-02-10T12:01:27.140 回答
7

我遇到了类似的问题,问题是init()重构我的代码后导致崩溃的方法,它就像一个魅力。也许它会帮助你:

#if !TARGET_INTERFACE_BUILDER
required init(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    self._setup()
}
#endif

override func prepareForInterfaceBuilder() {
    self._setup()
}
于 2014-10-28T12:28:42.020 回答