Short answer

Set returnKeyType to .next on every field except the last, which gets .done. In textFieldShouldReturn(_:), move focus to the next field with becomeFirstResponder(), or close the keyboard and submit on the last one. To close the keyboard by tapping outside, add a tap gesture that calls view.endEditing(true) and set its cancelsTouchesInView to false so buttons keep working.

SWIFT TIPS · UIKIT

First posted on April 16, 2018 for Swift 4, as "Custom Keyboard For textFIeld ( Swift 4)". Rewritten on November 17, 2026. The Swift code below was type-checked on a Mac with Xcode 26.2 against the iOS 26.2 SDK, in Swift 5 and Swift 6 language modes, with iOS 15 as the deployment target. It wasn't run on a device.

The title said "custom keyboard", but the 2018 post was about something more common: a form with two text fields where the Return key on the first one says Next and jumps to the second, the second says Done and closes the keyboard, and a tap anywhere on the screen closes it too. I set the Return key types in the storyboard, wrote textFieldShouldReturn, and added a tap gesture recognizer with a didTapScreen action.

That's still how it's done. The version below handles any number of fields, sets the right keyboard for each one, and fixes the one thing a screen-wide tap gesture usually breaks.

A form that moves from field to field

import UIKit

final class SignUpViewController: UIViewController, UITextFieldDelegate {
    @IBOutlet private weak var nameField: UITextField!
    @IBOutlet private weak var emailField: UITextField!
    @IBOutlet private weak var passwordField: UITextField!
    @IBOutlet private weak var scrollView: UIScrollView!

    private var fieldsInOrder: [UITextField] { [nameField, emailField, passwordField] }

    override func viewDidLoad() {
        super.viewDidLoad()
        for (i, field) in fieldsInOrder.enumerated() {
            field.delegate = self
            field.returnKeyType = i == fieldsInOrder.count - 1 ? .done : .next
        }
        emailField.keyboardType = .emailAddress
        emailField.textContentType = .emailAddress
        emailField.autocapitalizationType = .none
        passwordField.textContentType = .newPassword
        passwordField.isSecureTextEntry = true

        // Tap outside a field to close the keyboard, without blocking buttons
        let tap = UITapGestureRecognizer(target: self, action: #selector(backgroundTapped))
        tap.cancelsTouchesInView = false
        view.addGestureRecognizer(tap)

        // Or let a drag in the scroll view close it
        scrollView.keyboardDismissMode = .interactive
    }

    @objc private func backgroundTapped() {
        view.endEditing(true)
    }

    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        if let i = fieldsInOrder.firstIndex(of: textField), i + 1 < fieldsInOrder.count {
            fieldsInOrder[i + 1].becomeFirstResponder()
        } else {
            textField.resignFirstResponder()
            submit()
        }
        return false
    }

    private func submit() {
        print("submit")
    }
}

How the Return key works

returnKeyType only changes the label on the key. Apple's documentation describes it as "the visible indication of what the Return key does". What actually happens when it's pressed is up to textFieldShouldReturn(_:).

The 2018 code checked textField1.isFirstResponder and hard-coded the second field. With three or more fields that turns into a chain of if statements. Here, fieldsInOrder lists the fields once. The Return handler finds the current field in that list and focuses the next one. On the last field it closes the keyboard and calls submit(), so pressing Done finishes the form instead of just hiding the keyboard. Adding a field means adding it to the list, nothing else.

The method returns false in both cases. That tells the text field not to do its own default handling, since the code has already decided what happens.

Closing the keyboard by tapping outside

The 2018 version checked which field was first responder and resigned that one. view.endEditing(true) does the same for any field inside the view, so the action is one line no matter how many fields there are.

The line that matters is tap.cancelsTouchesInView = false. Apple's documentation says that when this property is true, which is the default, touches that the gesture recognizes aren't delivered to the view underneath. With a tap recognizer on the whole screen, that can swallow taps meant for a button or a table row. Setting it to false lets the tap close the keyboard and still reach the control.

For forms inside a scroll view, keyboardDismissMode is a gentler option. With .interactive, dragging the content down pulls the keyboard down with your finger. Apple's documentation lists none as the default, so you have to set it.

The right keyboard for each field

The code also sets up the fields in ways the 2018 post didn't:

  • keyboardType = .emailAddress shows a keyboard with @ and . on the main layer.
  • textContentType tells iOS what the field holds. .emailAddress lets it suggest the user's address, and .newPassword marks a sign-up password field, where Password AutoFill can suggest a strong password if your app is set up for it.
  • autocapitalizationType = .none stops an email address from starting with a capital letter.

Each of these is one line, and together they save the user more typing than any custom keyboard would.

A quick check for your project

  1. Open each form and press Return on every field. Each one should either move to the next field or finish the form.
  2. Search for UITapGestureRecognizer added to view. If cancelsTouchesInView isn't set to false, try tapping a button while the keyboard is up.
  3. Check that email, password and one-time code fields have a textContentType.

What the 2018 version said

The original had two outlets, textField1 and textField2, a textFieldShouldReturn that moved from the first to the second or resigned the second, and a didTapScreen action connected to a tap gesture recognizer in the storyboard, with screenshots of the Return Key setting in Interface Builder. The logic is the same as above, written for two fields.

Sources