I am creating a UIButton subclass to add some drawing . It is working fine in code . It is giving following error when i am trying via @IBdesignable .
error: IB Designables: Failed to update auto layout status: The agent crashed
error: IB Designables: Failed to render instance of DashboardHeaderView: The agent crashed
Following is code sample
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();
Agent crashing error is at
let context:CGContextRef = UIGraphicsGetCurrentContext()!
I overirde init + drawRect + initilizeButtonDrawings but got nothing positive
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()
}
All this is working fine with Objective C but crashing in swift
I solved this via following update...
let rect:CGRect = CGRectMake(0, 0, iconSize, iconSize)
UIGraphicsBeginImageContextWithOptions(rect.size, false, 0.0);
let context = UIGraphicsGetCurrentContext()
// draw icon
var iconPath:UIBezierPath!
let iconRect:CGRect = CGRectMake(iconStrokeWidth / 2, iconStrokeWidth / 2, iconSize - iconStrokeWidth, iconSize - iconStrokeWidth);
if self.iconSquare {
iconPath = UIBezierPath(rect:iconRect )
} else {
iconPath = UIBezierPath(ovalInRect:iconRect)
}
iconColor.setStroke()
iconPath.lineWidth = iconStrokeWidth;
iconPath.stroke()
CGContextAddPath(context, iconPath.CGPath);
let image:UIImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();