How to hide keyboard when using SwiftUI?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In SwiftUI, the best way to hide the keyboard depends on the iOS version and the shape of the screen. On modern iOS, @FocusState is the preferred approach because the keyboard follows view state cleanly. For older compatibility or UIKit interop, you can still dismiss the current responder manually, but that should usually be the fallback rather than the default design.
The Preferred Modern Approach: @FocusState
On iOS 15 and later, clear the focus to dismiss the keyboard.
Setting focusedField = nil removes focus and hides the keyboard.
Dismiss on Background Tap
Many apps also dismiss the keyboard when the user taps outside the field.
The contentShape(Rectangle()) call helps the tap gesture cover empty layout space too.
A Toolbar Button Helps on Number Pads
Numeric keyboards often have no return key, so a toolbar button is a good dismissal affordance.
This improves usability even if the rest of the screen does not use focus-state-based dismissal.
UIKit Fallback for Older or Mixed Setups
If the app mixes SwiftUI with UIKit or supports older patterns, responder-chain dismissal still works.
Then call it from a button or gesture.
This is practical, but it is less explicit than focus management.
Think About Layout and Navigation Too
Keyboard dismissal is not just about hiding the keyboard. It is also about keeping forms usable during focus changes, navigation, and screen-size changes.
Good practices include:
- testing on small devices
- checking portrait and landscape behavior
- clearing focus on submit or route transitions when appropriate
- making sure tap gestures do not interfere with controls
A keyboard that technically dismisses but causes layout flicker or blocked taps is still a UX problem.
Common Pitfalls
- Using UIKit responder tricks everywhere instead of adopting
@FocusStateon modern SwiftUI screens. - Forgetting to clear focus after submit actions.
- Adding a broad tap gesture that interferes with buttons or list interactions.
- Providing no dismissal path for decimal or number-pad keyboards.
- Testing only one screen size and missing overlap or scroll issues on smaller devices.
Summary
- '
@FocusStateis the preferred SwiftUI keyboard-control mechanism on modern iOS.' - Clearing focus dismisses the keyboard cleanly.
- Background taps and keyboard toolbars improve usability for form-heavy screens.
- UIKit responder-chain dismissal is still useful as a fallback.
- Good keyboard behavior includes layout, focus, and navigation polish, not only the hide action itself.
Related reading
- How to hide soft keyboard on android after clicking outside EditText?
- How to hide the back button in UINavigationController?
- How to hide the keyboard when I press return key in a UITextField?
- How to hide the title bar for an Activity in XML with existing custom theme
- How to hide the title bar for an Activity in XML with existing custom theme
- How to hide UINavigationBar 1px bottom line
- How to hide UINavigationBar 1px bottom line
- How to hide underbar in EditText
.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.