Programmatically align a toolbar on top of the iPhone keyboard
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Adding a toolbar above the iPhone keyboard improves form usability by giving users quick actions such as Next, Done, or formatting commands. The easiest and most stable approach is to attach a UIToolbar as an input accessory view. For complex layouts, keyboard notifications can help keep additional UI elements aligned with keyboard movement.
Use inputAccessoryView for Text Fields and Text Views
For most cases, assign a toolbar directly to each input control.
The system automatically keeps the accessory view attached to the keyboard top.
Add Previous and Next Navigation for Multi-Field Forms
In forms with several inputs, toolbar actions can move focus between fields.
Each field gets its own accessory configured with index context.
Align a Separate Toolbar View with Keyboard Notifications
If you need a standalone toolbar not attached to input view, observe keyboard frame changes and update constraints.
This keeps custom bottom UI synchronized with keyboard animation.
Handle Safe Areas and Rotation
On modern iPhones, safe area insets and orientation changes affect available bottom space. Test keyboard toolbar behavior in portrait and landscape modes.
For accessory views, the system handles most geometry. For manual constraints, recompute offsets after rotation and scene size changes.
Also validate with hardware keyboard connected, where software keyboard may not appear.
SwiftUI Interop Note
If your app is mostly SwiftUI but uses UIKit text inputs in wrappers, you can still attach an accessory toolbar by exposing the underlying UITextField or UITextView from UIViewRepresentable. Keep toolbar creation in the coordinator so actions can call SwiftUI bindings safely.
This keeps a consistent keyboard-toolbar experience even in mixed UIKit and SwiftUI codebases.
Common Pitfalls
A common mistake is adding a toolbar as a regular subview and expecting it to follow keyboard automatically. Without keyboard notifications or input accessory usage, alignment will break.
Another issue is not animating constraint updates with keyboard timing values, causing visible jumpy motion.
Developers also forget to remove observers for long-lived controllers, which can produce duplicate callbacks. Use lifecycle-safe observer management.
Summary
- Use
inputAccessoryViewfor the simplest keyboard-top toolbar behavior. - Add Next and Done controls to improve multi-field navigation.
- Use keyboard frame notifications for custom standalone toolbars.
- Animate layout updates with keyboard animation duration and curve.
- Test across orientations, safe areas, and hardware keyboard scenarios.

