Overview
IQKeyboardManager uses a sophisticated notification-based system to automatically adjust your UI when the keyboard appears. It monitors keyboard and text input notifications, calculates the required adjustments, and manipulates the view hierarchy to keep text fields visible.How It Works
Notification System
IQKeyboardManager observes three key notification sources:- Keyboard Notifications - Tracks keyboard show/hide/change events via
IQKeyboardNotification - Text Input Notifications - Monitors text field/text view focus changes via
IQTextInputViewNotification - Application State - Responds to app becoming active to refresh adjustments
Active Configuration
TheIQActiveConfiguration class coordinates all keyboard management state:
- Current keyboard info - Frame, animation duration, visibility
- Active text input view - Which field is currently focused
- Root configuration - View controller and initial layout state
IQActiveConfiguration acts as the central coordinator, determining when to call
adjustPosition() or restorePosition() based on keyboard and text input events.Position Adjustment Logic
When a text input becomes active and the keyboard shows, IQKeyboardManager follows this flow:1. Calculate Keyboard Size
- Adding the configured
keyboardDistance - Adding bottom safe area insets
- Intersecting with window frame (handles hardware keyboards)
2. Calculate Move Distance
The library calculates how much to move the view upward:3. Apply Adjustments
IQKeyboardManager applies adjustments in order of priority: ScrollView Content Offset (if text field is in a scroll view)View Hierarchy Management
Finding the Root Controller
IQKeyboardManager walks up the view hierarchy to find the root view controller:- Navigation controllers
- Tab bar controllers
- Page view controllers
- Modal presentations
ScrollView Handling
The library has special logic for nested scroll views:- Walks hierarchy to find the nearest scrollable parent
- Stores original state in
IQScrollViewConfiguration - Adjusts content offset to reveal the text field
- Restores state when keyboard dismisses
TableView & CollectionView
For table and collection views, IQKeyboardManager:- Detects the cell containing the text field
- Finds the previous cell’s index path
- Adjusts scroll offset to show the previous cell (for context)
Distance Calculation
Global Distance
Set the default distance for all text fields:Per-View Distance
Override for specific text fields using the.iq extension:
How distance priority works
How distance priority works
The library checks distances in this order:
- SearchBar distance - If text field is in a UISearchBar
- View-specific distance - Set via
view.iq.distanceFromKeyboard - Global distance - Fallback to
IQKeyboardManager.shared.keyboardDistance
Enable/Disable Behavior
Global Enable/Disable
restorePosition() to reset all view adjustments.
Class-Level Control
Disable keyboard management for specific view controller types:If a class appears in both
disabledDistanceHandlingClasses and enabledDistanceHandlingClasses, the disabled setting takes precedence.Per-View Control
Use the.iq.enableMode property for fine-grained control:
Per-View Customization
All per-view customization uses the.iq namespace extension:
Per-Controller Customization
View controllers can be disabled using class-level arrays:Performance Considerations
Main Thread Enforcement
All IQKeyboardManager APIs are marked with@MainActor to ensure thread safety:
Animation Synchronization
Adjustments are animated to match keyboard animation timing:Layout Updates
By default, IQKeyboardManager doesn’t force layout updates. Enable if needed:State Restoration
The library efficiently tracks original states to restore views:IQRootControllerConfiguration- Stores root controller origin and safe areaIQScrollViewConfiguration- Stores scroll view content offset and insetsmovedDistance- Tracks total adjustment applied
Interactive Gesture Handling
Special handling for interactive navigation gestures:Manual Refresh
If you make programmatic layout changes while the keyboard is visible, call:When to call reloadLayoutIfNeeded()
When to call reloadLayoutIfNeeded()
Call this method after:
- Programmatically changing view frames
- Adding/removing views while keyboard is visible
- Modifying Auto Layout constraints affecting text field position
- Custom orientation changes
Next Steps
Configuration
Learn about configuration architecture and settings
Subspecs
Understand the modular subspec architecture