TextFields with inputView

:  ~ 45 sec read

Let's say we have a textField with an UIPickerView as its inputView.

The first problem is that we want the textField to not be editable, because we populate it with the value picked from the picker. This is the simple part:

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
  return emailTextField == textField

The second problem is that the textField is still tappable, it still gets focus and the caret is still visible.

We can't return true on shouldBeginEditing, we can't disable interactions, and resigningFirstResponder doesn't help either, since it closes the picker, and the whole idea is to use the picker natively, without creating a new set of methods that showPicker and hidePicker.

Since a textField's tintColor affects its caret too, the solution for our second problem turned out simple, after all:

emailTextField.tintColor = .clearColor()

Bonus tip:

private var email = "" {
  didSet { emailTextField.text = oldValue }

This will automatically update the textField whenever email changes.