Trevor Wood Trevor Wood - 1 year ago 61
Swift Question

Is it possible to select all text fields in a view?

So my code looks like this, targeting each known textfield and applying the same modifier to them.

Is there some way I can just select all textfields in the view?

import UIKit

class CreateAccount: UIViewController, UITextFieldDelegate, UIScrollViewDelegate {

@IBOutlet weak var scrollViewer: UIScrollView!
@IBOutlet weak var usernameTextField: UITextField!
@IBOutlet weak var emailTextField: UITextField!
@IBOutlet weak var passwordTextField: UITextField!
@IBOutlet weak var reenterPasswordTextField: UITextField!

let textFieldBorderColor: UIColor = UIColor(red: 170.0/255.0, green: 170.0/255.0, blue: 170.0/255.0, alpha: 1.0)
let textFieldBorderFocusedColor: UIColor = UIColor(red: 31.0/255.0, green: 111.0/255.0, blue: 217.0/255.0, alpha: 1.0)

override func viewDidLoad() {
// Do any additional setup after loading the view.

usernameTextField.layer.borderColor = textFieldBorderColor.CGColor
emailTextField.layer.borderColor = textFieldBorderColor.CGColor
passwordTextField.layer.borderColor = textFieldBorderColor.CGColor
reenterPasswordTextField.layer.borderColor = textFieldBorderColor.CGColor

usernameTextField.layer.borderWidth = 1.0
emailTextField.layer.borderWidth = 1.0
passwordTextField.layer.borderWidth = 1.0
reenterPasswordTextField.layer.borderWidth = 1.0

scrollViewer.delegate = self


Answer Source

You may need to subclass a UITextField and then inherit from this subclass all of your UITextField elements.

Then you will need to apply properties / styles for this subclass only.

For example (written in Swift 3.0):

class MyCustomTextField: UITextField {
    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        self.layer.borderWidth = 3.0
        self.layer.borderColor =

@IBOutlet weak var usernameTextField: MyCustomTextField!

func viewDidLoad () {
    // you don't need to apply style properties here anymore