Move a view up only when the keyboard covers an input field
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
The goal is not to move the whole screen every time the keyboard appears. The goal is to move content only when the active input field would actually be covered. The clean way to do that is to detect the keyboard frame, find the active field's position, calculate the overlap, and adjust only as much as necessary.
Prefer adjusting a constraint or scroll inset
On iOS, moving the root view frame directly is usually the least flexible option. A better pattern is to either:
- adjust a bottom constraint
- update a scroll view inset
- scroll the active field into view
If your UI is form-like, a scroll view is often the easiest long-term answer. But if you already have a fixed layout and need a targeted upward shift, a bottom constraint works well.
Track the active input field
You need to know which text field or text view is being edited so you can measure whether that control is covered.
Without the active field reference, you cannot tell whether the keyboard is actually hiding the control the user is editing.
Calculate overlap instead of guessing
The right shift is based on geometry, not a hard-coded keyboard height. Convert both the keyboard frame and the active field frame into the view's coordinate system, then calculate the overlap.
If overlap is zero, the field is already visible and the layout stays put. That is the key behavior the title is asking for.
Reset when the keyboard is gone
When editing finishes or the keyboard hides, restore the original layout instead of leaving the view shifted.
In practice, many apps handle both show and hide from keyboardWillChangeFrameNotification, because the keyboard frame moves to the bottom of the screen when hiding. The important part is restoring the original constraint once there is no overlap.
Why this is better than moving the whole root view
Directly shifting view.frame.origin.y is tempting, but it tends to fight Auto Layout, safe areas, and nested containers. Constraint-based movement is easier to animate and easier to reason about when the screen rotates or the keyboard changes height.
That is also why scroll views remain the most robust option for larger forms. They solve the same visibility problem without pretending the whole screen must move as one block.
Common Pitfalls
The biggest mistake is moving the UI every time the keyboard appears, even when the active field is already visible. That creates distracting unnecessary motion.
Another issue is using a hard-coded keyboard height. Keyboard size changes with device, orientation, hardware keyboards, and input method.
Developers also forget coordinate conversion. Keyboard frames arrive in screen coordinates, while input fields are often measured in a local view hierarchy.
Finally, changing the root view frame directly often causes more layout problems than it solves in Auto Layout-driven screens.
Summary
- Track the active input field so you know what needs to stay visible.
- Compare the keyboard frame with the active field's frame and calculate the actual overlap.
- Move the layout only by the amount needed, or not at all if there is no overlap.
- Prefer bottom-constraint or scroll-view adjustments over moving the whole root view frame.
- Reset the layout when the keyboard hides or the field is no longer active.
Related reading
- Move a view up only when the keyboard covers an input field
- Move or copy view controller from one storyboard to another
- Move TextField up when the keyboard has appeared in SwiftUI
- Move TextField up when the keyboard has appeared in SwiftUI
- Move textfield when keyboard appears swift
- Move textfield when keyboard appears swift
- Move to another EditText when Soft Keyboard Next is clicked on Android
- Move view with keyboard using Swift
.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.