我正在尝试实现一个IBDesignable的简单UIButton子类.我希望能够从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,context: nil) } }
解决方法
原文地址:https://www.jb51.cc/iOS/332601.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。