我正在创建一个 UIButton 子类来添加一些绘图。它在代码中运行良好。当我通过 @IBdesignable 尝试时,它给出了以下错误。
错误:IB Designables:无法更新自动布局状态:代理崩溃
错误:IB Designables:无法呈现 DashboardHeaderView 的实例:代理崩溃
以下是代码示例
let context:CGContextRef = UIGraphicsGetCurrentContext()!
UIGraphicsPushContext(context)
UIGraphicsBeginImageContextWithOptions(rect.size, false, 0.0);
var path:UIBezierPath!
let pathRect:CGRect = CGRectMake(strokeWidth / 2, strokeWidth / 2, pathSize - iconStrokeWidth, pathSize - iconStrokeWidth);
path = UIBezierPath(rect: pathRect )
iconColor.setStroke()
iconPath.lineWidth = iconStrokeWidth;
iconPath.stroke()
CGContextAddPath(context, iconPath.CGPath);
let image:UIImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsPopContext();
UIGraphicsEndImageContext();
代理崩溃错误发生在
let context:CGContextRef = UIGraphicsGetCurrentContext()!
我忽略了 init + drawRect + initilizeButtonDrawings 但没有得到任何积极的东西
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
self.initilizeButtonDrawings()
}
override init(frame: CGRect) {
super.init(frame: frame)
self.initilizeButtonDrawings()
}
override func drawRect(rect:CGRect) {
super.drawRect(rect)
self.initilizeButtonDrawings()
}
override func prepareForInterfaceBuilder () {
self.initilizeButtonDrawings()
}
所有这些在Objective C中都可以正常工作,但很快就会崩溃