How do you dismiss the keyboard when editing a UITextField
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
On iOS, dismissing the keyboard for a UITextField means telling the active control to resign first responder status. The most common solutions are dismissing on the Return key, dismissing when the user taps outside the field, or dismissing the whole view hierarchy with view.endEditing(true).
Dismiss on the Return key
The standard pattern is to make the view controller the text field’s delegate and implement textFieldShouldReturn.
This is the right solution when pressing Return should simply close the keyboard for that field.
Move to the next field in a form
In forms with several inputs, Return often should move focus instead of dismissing immediately.
This usually feels better than dismissing after every field because it keeps the form flow uninterrupted.
Dismiss when tapping outside
Users also expect the keyboard to disappear when they tap elsewhere on the screen. A tap gesture recognizer is a common solution.
view.endEditing(true) asks the entire view hierarchy to make the current first responder resign, so it works even if more text fields are added later.
resignFirstResponder versus endEditing
These two methods solve related problems:
- '
textField.resignFirstResponder()targets one known text field' - '
view.endEditing(true)dismisses whichever editable control is currently active'
If you are in the text field delegate and know exactly which field is active, resignFirstResponder() is fine. If you are handling a background tap, save button, or generic dismissal action, endEditing(true) is often simpler.
Dismiss from a button action
You can also dismiss the keyboard when a button is tapped:
This is useful when form submission and keyboard dismissal should happen together.
Make the keyboard behavior intentional
Keyboard dismissal is partly a UX choice, not just a code choice. A good form often also sets appropriate return key types:
That gives users a clearer signal about whether Return moves forward or finishes editing.
Also remember to keep the active field visible if the keyboard covers it. Dismissing the keyboard is only part of making text entry feel polished.
Common Pitfalls
The biggest mistake is forgetting to set the text field delegate. If the delegate is not assigned, textFieldShouldReturn will never be called.
Another issue is using a tap recognizer that blocks other touch handling. Setting cancelsTouchesInView incorrectly can interfere with buttons, table cells, or collection views.
Developers also call resignFirstResponder() on a specific field even though another field is actually active. In those cases, view.endEditing(true) is more reliable.
Finally, dismissing the keyboard immediately on every Return key can be the wrong UX for multi-field forms. Moving to the next field often feels more natural.
Summary
- Dismissing the keyboard means making the current first responder resign.
- '
textFieldShouldReturnis the standard solution for Return-key dismissal.' - '
view.endEditing(true)is a convenient generic dismissal method.' - Tapping outside the field is commonly handled with a gesture recognizer.
- In multi-field forms, moving to the next field is often better than dismissing immediately.
Related reading
- How do you display a Toast from a background thread on Android?
- How do you document the parameters of a function's closure parameter in Swift 3?
- How do you draw a line programmatically from a view controller?
- How do you dynamically add elements to a ListView on Android?
- How do you find out the type of an object in Swift?
- How do you find out the type of an object in Swift?
- How do you get an iPhone's device name
- How do you hide the warnings in React Native iOS simulator?
.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.