2015-12-11 3 views
0

Я создаю подкласс UIButton для добавления некоторого рисунка. Он отлично работает в коде. Он дает следующую ошибку, когда я пытаюсь через @IBdesignable.CGContextRef не работает с @IBDesignable (Ошибка: агент разбился)

  1. ошибка: IB Designables: Не удалось обновить статус Автокомпоновка: агент разбился

  2. ошибка: 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()! 

Я overirde инициализации + 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 но сбой в быстрой

ответ

0

Я решил это путем следующего обновления ...

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