How do I dismiss the iOS keyboard?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
On iOS, the keyboard stays visible while a text input remains first responder. So dismissing the keyboard is really about making the current text field or text view resign first responder status. The right pattern depends on whether you want dismissal from the return key, a tap outside the field, or a scrolling gesture.
UIKit: use endEditing(true) for broad dismissal
If you want the whole screen to dismiss whichever input is active, view.endEditing(true) is the most practical approach.
This walks the view hierarchy and tells the active responder to resign. It is simple and works well when several input fields may be on screen.
Dismiss on the return key
For a UITextField, the common pattern is to adopt UITextFieldDelegate and resign first responder inside textFieldShouldReturn.
This is the standard solution when the user is expected to finish editing by pressing Return.
Dismiss when tapping outside the input
For forms, adding a tap gesture recognizer to the background is a common pattern:
Setting cancelsTouchesInView to false matters. Without it, the gesture can swallow button taps and make the UI feel broken.
Scroll views can dismiss automatically
If the screen uses a UIScrollView, UITableView, or UICollectionView, you can often get a better experience by dismissing the keyboard during scrolling.
This is especially effective for forms because it feels natural: the user scrolls, and the keyboard gets out of the way.
If you want a softer interaction on supported scroll views, .interactive can make the keyboard track the user's drag instead of disappearing all at once. That often feels better on long forms where the keyboard and content move together.
SwiftUI patterns
In SwiftUI, the modern approach is to use @FocusState when possible.
This is cleaner than sending responder actions through UIApplication because it fits SwiftUI's state-driven model.
You can also pair @FocusState with .onSubmit when pressing Return should close the keyboard immediately:
That keeps the dismissal behavior close to the field itself and works well for login and search forms.
Common Pitfalls
The biggest pitfall is solving only one dismissal path. A screen may need return-key dismissal, outside-tap dismissal, and scroll dismissal together to feel complete.
Another issue is forgetting to wire the delegate for UITextField, which makes textFieldShouldReturn never run.
Gesture recognizers can also interfere with normal interactions if cancelsTouchesInView is left at its default value. That can block buttons or table selection.
Finally, prefer @FocusState in SwiftUI over older UIKit-style hacks when you control the view code. It is easier to reason about and better aligned with modern iOS development.
Summary
- Dismissing the iOS keyboard means making the active input resign first responder.
- Use
view.endEditing(true)for broad UIKit dismissal. - Use
textFieldShouldReturnfor return-key behavior. - Use a background tap gesture or scroll dismissal for form screens.
- In SwiftUI, prefer
@FocusStatefor keyboard control.
Related reading
- How do I dispatch_sync, dispatch_async, dispatch_after, etc in Swift 3, Swift 4, and beyond?
- How do I display an alert dialog on Android?
- How do I display an alert dialog on Android?
- How do I display the current value of an Android Preference in the Preference summary?
- How do I do base64 encoding on iOS?
- How do I download the Android SDK without downloading Android Studio?
- How do I draw a circle in iOS Swift?
- How do I draw a shadow under a UIView?
.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.