AppDelegate 中的 UITextField 自定义不适用于 Swift [英] UITextField customization in AppDelegate not working for Swift
问题描述
我想通过 AppDelegate 一次自定义所有 textFields,我已经测试了在单个 ViewController 上工作的自定义,但是当我通过 AppDelegate 添加自定义时,它不会出现.请指导我了解我错过了什么:
I want to customise all the textFields at once via AppDelegate , I had tested the customisation working on individual ViewControllers but when I add the customisation through AppDelegate then it does not appears. Please guide me for what have I missed :
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
UITextField.appearance().layer.borderWidth = 2
UITextField.appearance().layer.borderColor = UIColor(red:0.094 , green:0.737 , blue:0.612 , alpha:1).CGColor
return true
}
推荐答案
我找到了解决方案,感谢@nannav.与其在 AppDelegate 中自定义 TextField,不如创建一个新类,如:
I found the solution , thanx to @nannav. Rather than customising the TextField in AppDelegate, we should create a new class like :
class MyCustomTextField: UITextField {
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.layer.borderWidth = 2
self.layer.borderColor = UIColor(red:0.094 , green:0.737 , blue:0.612 , alpha:1).CGColor
}
}
稍后在 storyBoard 中,我们必须将 customClass 中的 MyCustomTextField 分配给我们想要自定义的文本字段
later in storyBoard we have to assign MyCustomTextField in customClass to the Textfields we wanted to customise
这篇关于AppDelegate 中的 UITextField 自定义不适用于 Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!