Alexander Deplov – Senior Product Designer Blog

How to Set Padding for UITextField in Swift, the Easiest Way

· Alexander Deplov

Of course, you also need to add a top and a bottom paddings, but this can be achieved through autolayout height size of the textfield. Result: left padding for UITextField placeholder and text:

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var myTextField: UITextField!

    override func viewDidLoad() {
        super.viewDidLoad()

        let paddingView = UIView(frame: CGRect(x: 0, y: 0, width: 15, height: myTextField.frame.height))
        myTextField.leftView = paddingView
        myTextField.leftViewMode = .always 
    }
}

We used this technique in our new Taskler app, but without big indent.

Let's connect on LinkedIn!