In addition to klevison-matias answer, this is the code I use when I want to add an add-on to my TextField in Swift 3
//UITextField : override textRect, editingRect class LeftPaddedTextField: UITextField { override func textRect(forBounds bounds: CGRect) -> CGRect { return CGRect(x: bounds.origin.x + 10, y: bounds.origin.y, width: bounds.width, height: bounds.height) } override func editingRect(forBounds bounds: CGRect) -> CGRect { return CGRect(x: bounds.origin.x + 10, y: bounds.origin.y, width: bounds.width, height: bounds.height) } }
Then in my TextField I use like this:
let emailTextField: LeftPaddedTextField = { let textField = LeftPaddedTextField() textField.placeholder = "Enter email" textField.layer.borderColor = UIColor.lightGray.cgColor textField.layer.borderWidth = 1 textField.keyboardType = .emailAddress return textField }() let passwordTextField: LeftPaddedTextField = { let textField = LeftPaddedTextField() textField.placeholder = "Enter password" textField.layer.borderColor = UIColor.lightGray.cgColor textField.layer.borderWidth = 1 textField.isSecureTextEntry = true return textField }()
source share