How can I change all TextField border colour in Swift 3? I've built one iPad application with many TextFields in my .xib file and now I want to change border colour, but it seems like so many lines to write a particular textfield
            Asked
            
        
        
            Active
            
        
            Viewed 8,017 times
        
    0
            
            
        - 
                    you can make extension of textfield. – Piyush Sinroja Mar 27 '17 at 10:59
- 
                    use this :- http://stackoverflow.com/questions/34782693/properly-subclassing-uitextfield-in-swift – Abhishek Gupta Mar 27 '17 at 10:59
3 Answers
7
            Add this extension to create border for all textfields in your project.
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
    }
}
 
    
    
        RajeshKumar R
        
- 15,445
- 2
- 38
- 70
- 
                    
- 
                    1Use this extension http://stackoverflow.com/a/42573932/7250862 and replace this `self.layer.borderColor = UIColor.uicolorFromHex(999999, alpha: 1)).cgColor` – RajeshKumar R Mar 29 '17 at 09:22
2
            
            
        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
}}
 
    
    
        Piyush Sinroja
        
- 160
- 9
- 
                    
- 
                    i want to change every TextField borderColor in my application more than 5000+ TextField – Harshil Kotecha Mar 27 '17 at 11:30
2
            
            
        You should create a new class which is subclass of UITextField as this :
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
    }
}
Now open xib select all text fields.
In identity inspector, change the custom class to YourTextField
This way even you have 1000 text fields in you project, no need to write even one more line for this purpose.
 
    
    
        Jagdeep Singh
        
- 2,556
- 3
- 18
- 28
 
    