Swift
iOS Development
UITextField
Keyboard Type
Mobile Programming

Programmatically change UITextField Keyboard type

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

Changing a UITextField keyboard type programmatically is as simple as setting its keyboardType property, but there is one important runtime detail: if the text field is already first responder, the keyboard may not refresh until you reload the input views. That distinction is what usually makes this task feel inconsistent.

Set the Keyboard Type Directly

The core API is straightforward.

swift
1import UIKit
2
3let textField = UITextField()
4textField.keyboardType = .numberPad

Common keyboard types include:

  • '.default'
  • '.numberPad'
  • '.decimalPad'
  • '.emailAddress'
  • '.URL'
  • '.phonePad'

Choose the type that best matches the data you actually want the user to enter.

Example in a View Controller

Here is a small programmatic example where one button switches the field into email mode.

swift
1import UIKit
2
3final class ViewController: UIViewController {
4    let textField = UITextField(frame: CGRect(x: 20, y: 120, width: 280, height: 40))
5
6    override func viewDidLoad() {
7        super.viewDidLoad()
8        view.backgroundColor = .systemBackground
9
10        textField.borderStyle = .roundedRect
11        textField.keyboardType = .default
12        view.addSubview(textField)
13    }
14
15    func switchToEmailKeyboard() {
16        textField.keyboardType = .emailAddress
17    }
18}

If the field is not currently editing, that is enough. The new keyboard type will apply the next time the field becomes first responder.

Refresh the Keyboard While Editing

If the user is already focused in the field, change the type and then reload the input views.

swift
textField.keyboardType = .numberPad
textField.reloadInputViews()

Without reloadInputViews(), the field may keep showing the old keyboard until editing ends and begins again.

Change the Type Based on App State

A common use case is switching input behavior depending on which kind of data the user is entering.

swift
1func configureField(for mode: String) {
2    switch mode {
3    case "email":
4        textField.keyboardType = .emailAddress
5    case "phone":
6        textField.keyboardType = .phonePad
7    case "amount":
8        textField.keyboardType = .decimalPad
9    default:
10        textField.keyboardType = .default
11    }
12
13    if textField.isFirstResponder {
14        textField.reloadInputViews()
15    }
16}

This is better than trying to reuse one keyboard type for all input modes and validating everything afterward.

Keyboard Type Does Not Replace Validation

A keyboard type is a hint to the system keyboard, not a validation guarantee. Users can still paste unexpected content, use external keyboards, or encounter locale-specific input behavior.

That means you should still validate the final text value even if the keyboard looks correct.

For example, .numberPad does not prevent all possible invalid values in your business logic, and it also does not include a built-in return key in some contexts.

Test the Keyboard Flow, Not Only the Property Change

In forms with several fields, changing the keyboard type is only part of the behavior. You should also test focus movement, return-key handling, and first-responder transitions so the field change feels correct in the full user flow rather than only in isolation.

Common Pitfalls

  • Changing keyboardType while the field is active but forgetting reloadInputViews().
  • Treating keyboard type as full validation instead of only input guidance.
  • Using .numberPad for values that actually need decimals or signed input.
  • Forgetting that hardware keyboards and paste operations can bypass on-screen keyboard assumptions.
  • Reusing one text field for many modes without updating related behaviors such as placeholder text and validation rules.

Summary

  • Set keyboardType directly on the UITextField.
  • If the field is already first responder, call reloadInputViews() to refresh the visible keyboard.
  • Choose the keyboard type based on the real input requirement, not a rough approximation.
  • Keep validation logic separate from keyboard configuration.
  • Remember that keyboard type influences UX, but it does not enforce data correctness by itself.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.