Close iOS Keyboard by touching anywhere using Swift
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
A common iOS usability improvement is dismissing the keyboard when the user taps outside a text field. UIKit does not do this automatically for every screen, so you usually add a tap gesture recognizer or use one of the built-in keyboard dismissal behaviors provided by scroll views. The right solution depends on the screen, but the core idea is always the same: end editing on the current view hierarchy.
The Simple View-Controller Solution
The most common approach is to attach a tap gesture recognizer to the main view and call view.endEditing(true).
view.endEditing(true) asks the current first responder, such as a UITextField or UITextView, to resign. Once that happens, the keyboard disappears.
Why cancelsTouchesInView = false Matters
This detail is easy to miss. By default, a tap gesture recognizer can prevent the touched view from receiving its normal tap handling.
If you set:
then buttons and other controls still receive their touch events after the keyboard is dismissed. Without that line, you may create a subtle bug where tapping a button dismisses the keyboard but does not trigger the button action.
That is why most production implementations include it.
Make It Reusable with a Small Extension
If several screens need the same behavior, an extension keeps the setup concise:
Then in each controller:
This works well when most of the app uses the same keyboard-dismiss-on-background-tap behavior.
Scroll Views Have a Built-In Option Too
If your form is inside a UIScrollView, UITableView, or UICollectionView, another option is keyboardDismissMode.
Useful modes include:
- '
.none' - '
.onDrag' - '
.interactive'
This does not dismiss the keyboard on any tap, but it often creates a more natural experience for form-heavy screens where users scroll after editing.
Ignore Specific Touches When Necessary
Sometimes you do not want every tap to dismiss the keyboard. For example, you may want taps on certain controls to behave normally without the gesture recognizer interfering.
In that case, use a gesture recognizer delegate:
This gives you fine control when a global tap recognizer would be too blunt.
Prefer endEditing Over Tracking Individual Fields
A common beginner pattern is manually calling resignFirstResponder() on each field one by one. That quickly becomes repetitive.
view.endEditing(true) is usually better because it works regardless of which field is currently active. It also keeps the code resilient as the screen evolves and more editable controls are added.
Choose the Behavior That Matches the Screen
Not every screen should dismiss the keyboard the same way.
- login and settings screens often work well with tap-to-dismiss
- large forms inside scroll views often feel better with drag-to-dismiss
- highly interactive screens may need selective gesture filtering
The goal is not to add keyboard dismissal everywhere by reflex. The goal is to make the screen feel natural.
Common Pitfalls
One common mistake is forgetting cancelsTouchesInView = false, which can cause taps on buttons or table cells to stop working as expected.
Another mistake is adding multiple identical gesture recognizers over time, especially when setup code runs more than once. That can produce redundant behavior and make debugging confusing.
Developers also sometimes call resignFirstResponder() on one specific field even though another field is active. view.endEditing(true) is more robust.
Finally, on scroll-based forms, a tap recognizer may not be the best UX. Sometimes keyboardDismissMode is the cleaner built-in option.
Summary
- The usual Swift solution is a tap gesture recognizer that calls
view.endEditing(true). - Set
cancelsTouchesInView = falseso normal control taps still work. - For repeated use, wrap the setup in a reusable view-controller extension.
- On scroll views, consider
keyboardDismissModeas an alternative. - Choose the dismissal behavior that best matches the screen's interaction style.
Related reading
- Close iOS Keyboard by touching anywhere using Swift
- Closing a view displayed via a modal segue
- Closure cannot implicitly capture a mutating self parameter
- Cocoa Touch How To Change UIView's Border Color And Thickness?
- cocoapods - 'pod install' takes forever
- Cocoapods Cannot load underlying module for 'x
- CocoaPods could not find compatible versions for pod Firebase/Core” cloud_firestore, Flutter
- CocoaPods could not find compatible versions for pod Firebase/CoreOnly
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.