Tableview scroll content when keyboard shows
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
On iOS, text input inside a UITableView often fails in subtle ways when the keyboard appears. A text field near the bottom may be hidden, scrolling may jump, and insets may never reset after dismissal. These issues come from not coordinating keyboard notifications, safe-area insets, and current first responder position.
A robust implementation must do three things reliably: detect keyboard frame changes, adjust table insets to keep content reachable, and scroll the active input into view. It should also handle modern behaviors like interactive dismissal and different keyboard heights (hardware keyboard, QuickType, split keyboard on iPad). This guide shows a practical Swift approach that works in production forms and settings screens.
Core Sections
Observe keyboard frame notifications
Use keyboardWillChangeFrame instead of only keyboardWillShow/Hide, because frame changes happen during rotation and interactive transitions.
This single notification keeps logic centralized and less brittle.
Compute bottom inset relative to view coordinates
Keyboard frames in notifications are in screen coordinates. Convert before calculating overlap.
Using overlap logic avoids hard-coded keyboard heights.
Track and scroll the active input cell
When editing begins, capture the active input so you can scroll it into visible bounds.
Attach editingDidBegin to text fields/text views in cells. This improves user experience in long forms.
Handle dismissal and gesture behavior
A simple quality upgrade is letting users drag the table to dismiss keyboard.
Also ensure you reset insets when keyboard is gone (overlap becomes zero in the frame-change handler).
Prefer content insets over frame changes
Avoid manually resizing the table view frame for keyboard handling. Frame mutations can conflict with Auto Layout and safe areas. Insets are designed for this problem and play nicely with dynamic cell heights.
This keeps layout predictable across devices and orientation changes.
Common Pitfalls
- Listening only to
keyboardWillShowand missing intermediate frame changes during rotation or interactive dismissal. - Calculating overlap in screen coordinates without converting keyboard frame to the controller’s view space.
- Forgetting to update
scrollIndicatorInsets, which leaves scroll bars misaligned with visible content. - Resizing view frames directly, causing Auto Layout conflicts and jumpy animations.
- Not tracking the current first responder, so the user still cannot see the field they are editing.
Summary
Keyboard-safe table view forms require notification-driven inset updates plus focused scrolling to the active input. Use keyboardWillChangeFrame, convert coordinates correctly, compute overlap against safe area, and animate inset changes with the keyboard’s timing curve. Keep logic in one handler and avoid frame hacks. With these patterns, text input remains visible and smooth across device sizes, orientation changes, and modern keyboard behaviors.
Related reading
- TableView slow when adding images to cellForRowAtIndex
- Take iOS Simulator screenshots including device frame?
- Take screenshots in the iOS simulator
- Tap Action not working when Color is clear SwiftUI
- tap for more information or stop the app
- targetContentOffsetForProposedContentOffsetwithScrollingVelocity without subclassing UICollectionViewFlowLayout
- Task queue on Android like in GCD on iOS?
- Tensorflow-Lite pretrained model does not work in Android demo
.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.