Move TextField up when the keyboard has appeared in SwiftUI
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In SwiftUI, the keyboard can easily cover a TextField if your layout is fixed near the bottom of the screen. Newer SwiftUI containers handle some keyboard avoidance automatically, but custom forms, overlays, and bottom-aligned layouts often still need explicit adjustment. This article shows a practical keyboard-aware approach and explains when simpler layout choices are enough.
Start with the Simplest Layout
Before adding keyboard observers, check whether a ScrollView or Form solves the problem by itself. Those containers already cooperate better with the system keyboard than a rigid VStack.
If this layout already keeps fields visible, do not add custom keyboard code. Extra keyboard handling adds maintenance cost.
When You Need a Keyboard-Aware Offset
If the screen uses a bottom action bar or a custom stack, a keyboard height observer is usually the cleanest fix. The basic idea is:
- watch keyboard show and hide notifications
- store the keyboard height in state
- add bottom padding equal to that height
The observer below exposes the visible keyboard height in SwiftUI-friendly form.
This keeps UIKit keyboard details outside the view itself.
Build a Keyboard-Aware View
Apply the observer's value as bottom padding to shift the input region upward.
When the keyboard appears, the extra bottom padding moves the content upward and keeps the focused field visible.
Account for Safe Area and Home Indicator
Keyboard height includes the portion overlapping the screen, but your layout may also have bottom safe area insets. On some devices, applying full height can push content too far upward.
You can subtract the bottom inset when needed:
This usually produces more natural spacing on modern iPhones.
Dismissing the Keyboard
Moving the field up is only part of the experience. Users also need a clean way to dismiss the keyboard when they finish typing.
Then attach a tap gesture to the background if appropriate:
Use this carefully so it does not interfere with other gestures.
Prefer ScrollView for Larger Forms
If the screen has many fields, offsetting the entire view can become awkward. A ScrollView with focus management is often better because users can continue navigating without the whole interface jumping abruptly.
That pattern is especially useful for registration flows, profile editors, and checkout screens.
Common Pitfalls
- Adding keyboard observers when
FormorScrollViewwould already solve the issue. - Applying raw keyboard height without considering bottom safe area inset.
- Moving the whole screen when only the input region needs adjustment.
- Forgetting to animate the layout change, which makes the jump feel abrupt.
- Keeping custom keyboard code in many views instead of extracting a reusable observer.
Summary
- Start with
FormorScrollViewbefore building custom keyboard avoidance. - For custom layouts, observe keyboard notifications and add bottom padding.
- Subtract safe area inset when full keyboard height pushes content too far.
- Add a keyboard dismissal path so the form feels complete.
- Reuse a small
KeyboardObserverhelper instead of repeating UIKit glue code.

