gpt4 book ai didi

ios - 创建自定义 IBDesignable UITextField

转载 作者:行者123 更新时间:2023-11-29 11:43:01 25 4
gpt4 key购买 nike

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

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

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

Storyboard错误

error: IB Designables: Failed to update auto layout status: The agent crashed

代码:

@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 崩溃日志,它给出了以下错误 -

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

最佳答案

将此行更改为:

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

来自:

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

您不能将 UIColor 类型转换为 CGColor。像这样的强制转换会使应用程序崩溃。

关于ios - 创建自定义 IBDesignable UITextField,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45403790/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com