创建自定义IBDesignable UITextField

时间:2017-07-30 19:22:49

标签: ios swift3

我正在尝试创建自定义UITextField并设置自定义bordercolor。我也为borderColor创建了一个IBInspectable属性,如果我使用set和get方法,我可以使用Storyboard设置它。

现在我要做的是有一个预定义的边框颜色值(这是默认值)所以我试图调用didSet方法并在那里进行设置。我不知道该怎么做。默认值不起作用,如果我在我的故事板中导入行 -

,则会出错
self.layer.borderColor=(UIColor.KGColorPalette.entryBoxBorderColor as! CGColor) 

故事板中的错误

  

错误:IB Designables:无法更新自动布局状态:代理崩溃

代码:

@IBDesignable class KGIBDesignableTextField: UITextField {

 @IBInspectable var borderColor: UIColor = UIColor.KGColorPalette.entryBoxBorderColor {
    didSet {
        setup()
    }
}


override func awakeFromNib() {
    super.awakeFromNib()
    setup()
}
override func layoutSubviews() {
    super.layoutSubviews()
    setup()
}
override func prepareForInterfaceBuilder() {
    super.prepareForInterfaceBuilder()
    setup()
}


func setup(){
    textColor = UIColor.KGColorPalette.textEntryFontColor
    self.layer.borderColor=(UIColor.KGColorPalette.entryBoxBorderColor as! CGColor)   
    backgroundColor=UIColor.KGColorPalette.entryBoxShadeFillColor
    font = UIFont(name: "Helvetica", size: 14)
    textAlignment = .left


} 

}

如果它有用,我也检查了我的xcode Crash日志,它给出了以下错误 -

  

线程0崩溃:   0 com.kapsymtech.ios.KGCustomUIComponentsTarget 0x0000000116932dd8 _TFC26KGCustomUIComponentsTarget23KGIBDesignableTextField5setupfT_T_ + 152(KGIBDesignableTextField.swift:65)   1 com.kapsymtech.ios.KGCustomUIComponentsTarget 0x0000000116932acb _TFC26KGCustomUIComponentsTarget23KGIBDesignableTextFieldcfT5frameVSC6CGRect_S0_ + 331(KGIBDesignableTextField.swift:56)   2 com.kapsymtech.ios.KGCustomUIComponentsTarget 0x0000000116932ba7 _TToFC26KGCustomUIComponentsTarget23KGIBDesignableTextFieldcfT5frameVSC6CGRect_S0_ + 71   3 com.apple.IBCocoaTouchToolFoundation 0x0000000107a8fb23 - [UIView(IBCocoaTouchToolIntegration)initWithMarshalledValues:orderedKeys:ignoredKeys:globalMarshallingContext:] + 131   4 com.apple.IBCocoaTouchToolFoundation 0x0000000107aaf51c - [IBCocoaTouchToolObjectPackage initWithRequest:globalMarshallingContext:] + 3100   5 com.apple.IBCocoaTouchToolFoundation 0x0000000107aae143 + [IBCocoaTouchToolObjectPackage objectPackageFromRequest:] + 67   6 com.apple.IBCocoaTouchToolFoundation 0x0000000107ab1311 + [IBCocoaTouchToolObjectPackage decodeWithBinaryUnarchiver:] + 106   7 com.apple.dt.IBFoundation 0x000000010a493aa3 - [IBBinaryUnarchiver decodeObject] + 112   8 com.apple.dt.IBFoundation 0x000000010a51f724 - [IBObjectBasedMarshallingRequest initWithBinaryUnarchiver:] + 135   9 com.apple.dt.IBAutolayoutFoundation 0x000000010a3d73bb - [IBAutolayoutOperationMarshallingRequest initWithBinaryUnarchiver:] + 59   10 com.apple.dt.IBFoundation 0x000000010a48b335 + [NSObject(IBBinaryArchivingAdditions)decodeWithBinaryUnarchiver:] + 65   11 com.apple.dt.IBFoundation 0x000000010a493aa3 - [IBBinaryUnarchiver decodeObject] + 112   12 com.apple.dt.IBFoundation 0x000000010a559ffa - [IBMessageReceiveChannel deliverMessage:toTarget:withArguments:context:result:] + 393   13 com.apple.dt.IBFoundation 0x000000010a559a65 88- [IBMessageReceiveChannel runBlockingReceiveLoopNotifyingQueue:notifyingTarget:context:] _ block_invoke + 120   14 libdispatch.dylib 0x000000010dbe3792 _dispatch_client_callout + 8   15 libdispatch.dylib 0x000000010dbcc4e0 _dispatch_barrier_sync_f_slow_invoke + 292   16 libdispatch.dylib 0x000000010dbe3792 _dispatch_client_callout + 8   17 libdispatch.dylib 0x000000010dbcb247 _dispatch_main_queue_callback_4CF + 1041   18 com.apple.CoreFoundation 0x000000010bac1909 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE + 9   19 com.apple.CoreFoundation 0x000000010ba87ae4 __CFRunLoopRun + 2164   20 com.apple.CoreFoundation 0x000000010ba87016 CFRunLoopRunSpecific + 406   21 com.apple.Foundation 0x000000010a8f3600 - [NSRunLoop(NSRunLoop)runMode:beforeDate:] + 274   22 com.apple.dt.IBFoundation 0x000000010a525bbc - [IBAbstractPlatformTool startServingReceiveChannel:] + 465   23 com.apple.dt.IBFoundation 0x000000010a525d85 - [IBAbstractPlatformTool startServingWriteDescriptor:readDescriptor:] + 119   24 com.apple.dt.IBFoundation 0x000000010a52689e + [IBAbstractPlatformTool main] + 1186   25 IBDesignablesAgentCocoaTouch 0x0000000107a04ba7 main + 34   26 libdyld.dylib 0x000000010dc2c65d start + 1

1 个答案:

答案 0 :(得分:1)

将此行更改为:

self.layer.borderColor = UIColor.KGColorPalette.entryBoxBorderColor.cgColor

从:

self.layer.borderColor=(UIColor.KGColorPalette.entryBoxBorderColor as! CGColor) 

您无法将UIColor转换为CGColor。像这样的强制转换会使应用程序崩溃。