ScrollView and keyboard in Swift
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Forms inside a UIScrollView often break usability when the keyboard appears and hides active inputs. A reliable solution must handle inset updates, focused-field scrolling, animation timing, and observer lifecycle. Good keyboard handling is less about one callback and more about consistent behavior across rotation, device sizes, and input types.
Listen to Keyboard Frame Changes
keyboardWillChangeFrameNotification is the most practical event because it covers show, hide, and intermediate transitions.
This keeps keyboard and scroll behavior visually synchronized.
Scroll Active Input into View
Insets alone may not reveal the current field. Scroll to the focused control when editing begins.
Use the same strategy for UITextView and custom input controls.
Auto Layout and Safe Area Considerations
When the scroll view is constrained to safe areas, make sure inset updates apply to content and indicator insets consistently. If you use additional bottom accessory views, include their height in overlap calculations.
For iPhone models with varying bottom safe area, validate behavior in portrait and landscape. Keyboard height and visible overlap differ significantly across configurations.
Alternative Pattern with Keyboard Layout Guide
On newer iOS versions, keyboard layout guide can simplify manual calculations. It provides a layout anchor that follows keyboard movement.
This can reduce notification handling complexity, but test compatibility if you still support older OS versions.
Next and Done Accessory Controls
For long forms, adding an input accessory toolbar with Next and Done actions improves completion speed and reduces manual scrolling.
Accessory controls complement keyboard inset handling and provide a consistent form-navigation experience.
Testing Checklist
Useful test cases:
- first input near top and last input near bottom,
- multiline text view growth while keyboard is visible,
- rotation while editing,
- hardware keyboard attached,
- external language keyboard with different heights.
Automated UI tests can verify focus visibility by asserting element hittability after typing actions.
Common Pitfalls
- Updating insets only on keyboard show and missing frame-change transitions.
- Forgetting to remove observers and receiving duplicate callbacks.
- Scrolling wrong container when nested scroll views exist.
- Ignoring animation timing from keyboard notifications and causing jumpy UI.
- Handling only text fields while text views remain obscured.
Summary
- Use keyboard frame change notifications for robust inset handling.
- Keep focused controls visible by scrolling active inputs into view.
- Synchronize animations using keyboard duration and curve metadata.
- Account for safe area and device variation in overlap calculations.
- Validate behavior with rotation, multiline input, and hardware keyboard scenarios.
Related reading
- Scrollview vertical and horizontal in android
- SDK location not found. Define location with sdk.dir in the local.properties file or with an ANDROID_HOME environment variable
- SecItemAdd always returns error -34018 in Xcode 8 in iOS 10 simulator
- SecItemAdd and SecItemCopyMatching returns error code -34018 errSecMissingEntitlement
- Select all text inside EditText when it gets focus
- Selected tab's color in Bottom Navigation View
- selector in Swift?
- Selector syntax for swift 3.0
.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.