如何在Swift 3中更改所有的TextField边框颜色

如何在Swift 3中更改所有的TextField边框颜色,我构build了一个iPad应用程序。 我有我的.xib文件中的许多TextField,现在我想改变边框颜色,但它很多行写特定的文本字段,所以任何sorting的方式呢?

添加此扩展名为您的项目中的所有文本字段创build边框。

extension UITextField { open override func draw(_ rect: CGRect) { self.layer.cornerRadius = 3.0 self.layer.borderWidth = 1.0 self.layer.borderColor = UIColor.lightGray.cgColor self.layer.masksToBounds = true } } 
 extension UITextField { func cornerRadius(value: CGFloat) { self.layer.cornerRadius = value self.layer.borderWidth = 1.0 self.layer.borderColor = UIColor.lightGray.cgColor self.layer.masksToBounds = true }} 

你应该创build一个新的类,它是UITextField的子类,如下所示:

 import UIKit class YourTextField: UITextField { required init(coder aDecoder: NSCoder) { super.init(coder: aDecoder)! self.setBorderColor() } required override init(frame: CGRect) { super.init(frame: frame) self.setBorderColor() } func setBorderColor(){ self.layer.borderColor = .red // color you want self.layer.borderWidth = 3 // code which is common for all text fields } } 

现在打开xibselect所有文本字段。 在身份检查器中,将自定义类更改为YourTextField 。这样,即使您在项目中有1000个文本字段,也无需为此写入更多行。