IBDesignable UIButton子类

9

我正在尝试实现一个简单的UIButton子类,它是IBDesignable的。我想要能够从Interface Builder为控件的每个状态设置颜色。我知道使用IBInspectable关键字可以实现这一点。但是当我在state属性上使用KVO时,我的IB会崩溃。在deinit时,IBDesignable调试器会崩溃。有人知道我如何同时使用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)
    }

}
2个回答

9

Xcode 7.2下,用于@IBDesignable UIButton子类的通用代码如下:

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()
    }
}

我注意到如果你在init(frame:)中删除setupViews()的调用,IB和应用程序仍然可以正常工作。我们需要从那个init调用设置的原因是什么? - Paul Van Wieren
@PaulVanWieren 很有趣。我记得它不起作用。在 setupViews() 中设置断点并检查是谁在调用它 :) - skywinder
5
如果要在代码中创建视图,可以使用init(frame:)方法。在storyboards和nibs中创建的视图会使用init(coder:)方法。如果您永远不会在代码中创建视图,则不一定需要init(frame:)方法,但通常为了完整性而包括它。 - Jason Kirchner
节省了我很多时间和困难。 - Yitzchak
2
请注意,如果您使用任何@IBInspectable,则只会在“awakeFromNib”中运行时设置它们,因此您还需要添加以下内容:override func awakeFromNib(){ super.awakeFromNib() setupViews() }。也许您需要从init(withDecoder)中删除setupViews()吗? - Yitzchak

7

我曾经遇到类似的问题,原因是init()方法导致了崩溃。在重构代码之后,它就像魔术一样正常工作了。也许这对你有帮助:

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

override func prepareForInterfaceBuilder() {
    self._setup()
}

2
感谢Daniel的提示。我尝试了上面的方法,但现在它抱怨我的IBInspectable属性不符合键值编码规范。因此,我只能在运行时使用宏来添加/删除观察者。接受这个答案中的宏提示。 - Fergal Rooney

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接