Skip to main content

Overview

IQKeyboardManager is the primary class in the IQKeyboardManager library. It provides automatic keyboard management by preventing the keyboard from covering UITextField and UITextView controls. The class implements a singleton pattern and handles all keyboard interactions seamlessly without requiring manual code integration.
All public APIs must be called from the main thread. The class is marked with @MainActor to enforce this at compile time.

Singleton Access

let manager = IQKeyboardManager.shared
The shared instance is available throughout your application and should be configured once, typically in your AppDelegate or @main entry point.

Basic Usage

import IQKeyboardManagerSwift

@main
class AppDelegate: UIResponder, UIApplicationDelegate {
    func application(_ application: UIApplication,
                     didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Enable the keyboard manager
        IQKeyboardManager.shared.isEnabled = true
        
        // Optional: Configure keyboard distance
        IQKeyboardManager.shared.keyboardDistance = 20.0
        
        return true
    }
}

Configuration Properties

Core Settings

isEnabled
Bool
default:"false"
Enables or disables keyboard distance management globally.When enabled, the manager automatically adjusts the view hierarchy to prevent the keyboard from covering active text input views. When disabled, all position adjustments are reverted.
IQKeyboardManager.shared.isEnabled = true
keyboardDistance
CGFloat
default:"10.0"
The default distance (in points) between the keyboard and the active text input view.This global setting applies to all text inputs unless overridden using view.iq.distanceFromKeyboard for specific views.
The value must be non-negative. Negative values will be logged as warnings.
// Set global distance
IQKeyboardManager.shared.keyboardDistance = 20.0

// Override for specific text field
myTextField.iq.distanceFromKeyboard = 30.0

Animation Settings

layoutIfNeededOnUpdate
Bool
default:"false"
Determines whether to call setNeedsLayout() and layoutIfNeeded() on the view controller’s view during frame updates.Enable this when using Auto Layout constraints that need to be recalculated during keyboard adjustments.
IQKeyboardManager.shared.layoutIfNeededOnUpdate = true

Class-Level Configuration

disabledDistanceHandlingClasses
[UIViewController.Type]
An array of view controller classes for which keyboard distance handling should be disabled.When a view controller is of one of these types, keyboard distance handling is disabled regardless of the isEnabled property. This takes precedence over enabledDistanceHandlingClasses.
All classes must be subclasses of UIViewController.
// Disable for custom view controller
IQKeyboardManager.shared.disabledDistanceHandlingClasses.append(MyCustomViewController.self)

// Disable for multiple controllers
IQKeyboardManager.shared.disabledDistanceHandlingClasses += [
    LoginViewController.self,
    SignupViewController.self
]
enabledDistanceHandlingClasses
[UIViewController.Type]
default:"[]"
An array of view controller classes that should have keyboard distance handling force-enabled.Within this scope, the global isEnabled property is ignored. However, if a class appears in both disabledDistanceHandlingClasses and enabledDistanceHandlingClasses, it will be disabled.
IQKeyboardManager.shared.enabledDistanceHandlingClasses = [
    MySpecialViewController.self
]

Public Methods

reloadLayoutIfNeeded()

Manually triggers a position adjustment for the active text input view.
public func reloadLayoutIfNeeded()
Call this method when you’ve made external changes to the view hierarchy that might affect keyboard positioning (e.g., programmatically changing view frames, adding/removing views, changing constraints). When to Call:
  • After programmatically modifying view frames
  • After adding or removing views from the hierarchy
  • After changing Auto Layout constraints that affect the active text field’s position
  • When orientation changes occur outside the normal notification flow
Example:
// After programmatic layout changes
myView.frame = newFrame
IQKeyboardManager.shared.reloadLayoutIfNeeded()

// After constraint updates
NSLayoutConstraint.activate(newConstraints)
view.layoutIfNeeded()
IQKeyboardManager.shared.reloadLayoutIfNeeded()
This method only has an effect when:
  • isEnabled is true
  • A text input view is currently active
  • The keyboard is visible
  • The root configuration is ready
It is safe to call even when these conditions are not met - the method will simply return early.

Internal Configuration

The IQKeyboardManager class maintains an internal activeConfiguration property of type IQActiveConfiguration that tracks the current keyboard and text input state. This is used internally and is not exposed to public API consumers.

Thread Safety

All APIs are marked with @MainActor, ensuring they are always called on the main thread. Attempting to call these methods from a background thread will result in a compile-time error in Swift 6+ or a runtime assertion in earlier versions.
// ✅ Correct - called on main thread
Task { @MainActor in
    IQKeyboardManager.shared.isEnabled = true
}

// ❌ Incorrect - will cause issues
DispatchQueue.global().async {
    IQKeyboardManager.shared.isEnabled = true // Don't do this!
}

Availability

This class is unavailable in iOS App Extensions.
The class is marked with @available(iOSApplicationExtension, unavailable) because it relies on UIApplication APIs that are not available in app extensions.

See Also

Build docs developers (and LLMs) love