UITableView
iOS development
dismiss keyboard
UIKit
Swift programming

Dismiss keyboard by touching background of UITableView

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

To dismiss the keyboard when the user taps outside a text field in a UITableView, the usual solution is to end editing on the view hierarchy. In practice, that means either adding a tap gesture recognizer that calls view.endEditing(true) or using keyboardDismissMode for drag-based dismissal when scrolling is enough.

The simplest tap-based solution

A tap gesture recognizer attached to the table view or the controller's root view is the most common approach. The important configuration is cancelsTouchesInView = false, so normal cell selection and button taps still work.

swift
1import UIKit
2
3final class FormViewController: UITableViewController {
4    override func viewDidLoad() {
5        super.viewDidLoad()
6
7        let tap = UITapGestureRecognizer(target: self, action: #selector(dismissKeyboard))
8        tap.cancelsTouchesInView = false
9        tableView.addGestureRecognizer(tap)
10    }
11
12    @objc private func dismissKeyboard() {
13        view.endEditing(true)
14    }
15}

endEditing(true) walks the view hierarchy, asks the current first responder to resign, and dismisses the keyboard if an editable control is active.

Use keyboardDismissMode for scroll-based behavior

If the desired UX is "hide the keyboard when the user scrolls the table," UIKit already provides that directly.

swift
1override func viewDidLoad() {
2    super.viewDidLoad()
3    tableView.keyboardDismissMode = .onDrag
4}

This is especially good for forms inside a table view because users naturally drag the content after editing. It does not replace tap-to-dismiss in every design, but it is a good default.

Put the gesture at the right level

You can attach the tap recognizer to:

  • 'tableView'
  • 'view'
  • a dedicated background view

For most table-based forms, tableView is the easiest choice because it receives taps in the empty background area and alongside cells. If the screen includes controls outside the table, attaching the recognizer to the controller's main view may be more consistent.

The main design rule is to pick a level that matches the actual tappable background area.

Avoid breaking table interactions

The biggest mistake is adding a gesture recognizer that steals touches from cells, accessory buttons, or other controls. That is why cancelsTouchesInView = false matters.

If your table view has more complex gestures, you may also need to allow simultaneous recognition or restrict the recognizer to certain touch locations. The basic solution is usually enough, but do not assume every form screen has the same gesture complexity.

Consider the delegate method for return-key dismissal too

Tap-to-dismiss is only one part of keyboard UX. In text-field-heavy screens, also implement return-key behavior where appropriate.

swift
1extension FormViewController: UITextFieldDelegate {
2    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
3        textField.resignFirstResponder()
4        return true
5    }
6}

Good keyboard handling usually combines:

  • return-key dismissal
  • tap-to-dismiss
  • drag-to-dismiss when the view scrolls

Remember reused cells and responder state

With UITableView, text fields often live inside reusable cells. That means keyboard issues can sometimes be confused with cell reuse issues. Dismissing the keyboard hides the first responder, but you still need to manage data flow and cell configuration correctly so reused cells reflect the real model state.

Do not let keyboard handling become a bandage over reuse bugs.

Common Pitfalls

  • Adding a tap recognizer that cancels touches and breaks normal cell interaction.
  • Forgetting that keyboardDismissMode = .onDrag already solves the scroll-to-dismiss case.
  • Calling resignFirstResponder on one specific field instead of using view.endEditing(true) for the whole hierarchy.
  • Attaching the recognizer to the wrong view so empty background taps are not actually captured.
  • Blaming the keyboard when the real issue is bad table-cell reuse or focus management.

Summary

  • The usual tap solution is a gesture recognizer that calls view.endEditing(true).
  • Set cancelsTouchesInView = false so table interactions keep working.
  • Use tableView.keyboardDismissMode = .onDrag when scroll-based dismissal fits the UX.
  • Place the recognizer on the view layer that matches the real tappable background.
  • Combine tap dismissal with sensible return-key and cell-focus behavior for a clean form experience.

Course illustration
Course illustration

All Rights Reserved.