Reputation: 4141
I am trying to create a custom UITextField and set a custom bordercolor. I have created an IBInspectable property for borderColor as well and if i use the set and get methods for it, I am able to set it using Storyboard.
Now instead what i want to do is have a pre-defined border color value(which is the default value) so i am trying to call the didSet Method and do the setup there. I am not sure how to do that. The default value doesn't work and gives an error if I import in my storyboard in the line -
self.layer.borderColor=(UIColor.KGColorPalette.entryBoxBorderColor as! CGColor)
Error in Storyboard
error: IB Designables: Failed to update auto layout status: The agent crashed
Code:
@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
}
}
If it would help, I checked my xcode Crash logs as well and it gave the below error -
Thread 0 Crashed: 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
Upvotes: 1
Views: 861
Reputation: 2092
Change this line to:
self.layer.borderColor = UIColor.KGColorPalette.entryBoxBorderColor.cgColor
from:
self.layer.borderColor=(UIColor.KGColorPalette.entryBoxBorderColor as! CGColor)
You cant typecast UIColor
to CGColor
. A forced cast like that would crash the application.
Upvotes: 1