IBDesignable UIButton子类

Fer*_*ney 8 xcode xcode6 ibdesignable

我正在尝试实现一个简单的UIButton子类,它是IBDesignable.我希望能够从Interface Builder为控件的每个状态设置颜色.我知道这可以通过IBInspectable关键字实现.我在状态属性上使用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)
    }

}
Run Code Online (Sandbox Code Playgroud)

sky*_*der 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()
    }
}
Run Code Online (Sandbox Code Playgroud)

  • @PaulVanWieren - 如果在代码中创建视图,则使用init(frame :)方法.在故事板和笔尖中创建的视图使用init(编码器:)方法.如果您不打算在代码中创建视图,则不严格要求使用init(frame :)方法,但通常包含它是为了完整性. (5认同)
  • 请注意,如果您使用任何 @IBInspectable,您将仅在“awakeFromNib”中在运行时设置它们,因此您也需要添加此内容: override func awakeFromNib() { super.awakeFromNib() setupViews() }。也许你需要从 init(withDecoder) 中删除 setupViews() ??? (2认同)

Dan*_*iel 7

我有类似的问题是init()在重构我的代码之后导致崩溃的方法它像魅力一样工作.也许它会帮助你:

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

override func prepareForInterfaceBuilder() {
    self._setup()
}
Run Code Online (Sandbox Code Playgroud)

  • 感谢小费丹尼尔。我尝试了上述操作,但现在它抱怨我的IBInspectable属性不符合键值编码。因此,我仅在运行时使用宏来添加/删除观察者。接受有关MACRO提示的答案。 (2认同)