Skip to main content

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:
  1. Keyboard Notifications - Tracks keyboard show/hide/change events via IQKeyboardNotification
  2. Text Input Notifications - Monitors text field/text view focus changes via IQTextInputViewNotification
  3. Application State - Responds to app becoming active to refresh adjustments

Active Configuration

The IQActiveConfiguration 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

The keyboard size is adjusted by:
  • 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:
The moveUp value is positive when the text field is hidden behind the keyboard, and negative when it’s already visible.

3. Apply Adjustments

IQKeyboardManager applies adjustments in order of priority: ScrollView Content Offset (if text field is in a scroll view)
ScrollView Content Inset (for bottom padding)
Root View Controller Frame (as last resort)

View Hierarchy Management

Finding the Root Controller

IQKeyboardManager walks up the view hierarchy to find the root view controller:
This respects:
  • Navigation controllers
  • Tab bar controllers
  • Page view controllers
  • Modal presentations

ScrollView Handling

The library has special logic for nested scroll views:
  1. Walks hierarchy to find the nearest scrollable parent
  2. Stores original state in IQScrollViewConfiguration
  3. Adjusts content offset to reveal the text field
  4. 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:
The library checks distances in this order:
  1. SearchBar distance - If text field is in a UISearchBar
  2. View-specific distance - Set via view.iq.distanceFromKeyboard
  3. Global distance - Fallback to IQKeyboardManager.shared.keyboardDistance

Enable/Disable Behavior

Global Enable/Disable

When disabled, IQKeyboardManager calls 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:
Or configure at app launch:

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:
Setting layoutIfNeededOnUpdate = true may impact performance with complex layouts. Only enable if you experience layout issues during keyboard transitions.

State Restoration

The library efficiently tracks original states to restore views:
  • IQRootControllerConfiguration - Stores root controller origin and safe area
  • IQScrollViewConfiguration - Stores scroll view content offset and insets
  • movedDistance - Tracks total adjustment applied

Interactive Gesture Handling

Special handling for interactive navigation gestures:
During interactive pop gestures, IQKeyboardManager defers restoration until the gesture completes or cancels.

Manual Refresh

If you make programmatic layout changes while the keyboard is visible, call:
This recalculates and reapplies position adjustments.
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