Overview
While IQKeyboardManager works great with default settings, you may need to customize its behavior for specific view controllers or text fields. This guide covers advanced configuration options.Keyboard Distance Customization
Global Distance
Set the default distance between the keyboard and text fields for your entire app:AppDelegate.swift
The default value is
10.0 points. Negative values will trigger a warning log.Per-View Distance
Override the keyboard distance for specific text fields:ViewController.swift
Enable/Disable Per View
Control keyboard management for individual text fields usingenableMode:
ViewController.swift
Enable Modes
Uses the global
IQKeyboardManager.shared.isEnabled settingForce enables keyboard management for this view
Force disables keyboard management for this view
Class-Level Controls
Disable for Specific View Controllers
Prevent keyboard management for entire view controller classes:AppDelegate.swift
By default, these classes are disabled:
UITableViewControllerUIInputViewControllerUIAlertController
Force Enable for Specific Classes
Force enable keyboard management for specific view controller classes:AppDelegate.swift
Handling Edge Cases
Manual Layout Adjustments
If you make programmatic changes to the view hierarchy, callreloadLayoutIfNeeded():
ViewController.swift
Force Layout Updates
EnablelayoutIfNeededOnUpdate to force layout updates on every frame change:
AppDelegate.swift
This calls
setNeedsLayout() and layoutIfNeeded() on frame updates. Use sparingly as it may impact performance.Example: Complex Form
Here’s a complete example combining multiple configuration options:ComplexFormViewController.swift
Configuration Priority
The configuration system follows this priority order (highest to lowest):- View-specific
enableMode-.enabledor.disabled - Class-level disabled list -
disabledDistanceHandlingClasses - Class-level enabled list -
enabledDistanceHandlingClasses - Global enable -
IQKeyboardManager.shared.isEnabled - View-specific distance -
view.iq.distanceFromKeyboard - Global distance -
IQKeyboardManager.shared.keyboardDistance
Best Practices
Use global settings when possible
Use global settings when possible
Set global defaults in AppDelegate and only override for special cases. This keeps your codebase clean and maintainable.
Disable at the class level, not per instance
Disable at the class level, not per instance
If you need to disable IQKeyboardManager for a view controller, add it to
disabledDistanceHandlingClasses instead of disabling it in viewWillAppear and re-enabling in viewWillDisappear.Test edge cases thoroughly
Test edge cases thoroughly
Test your configuration with different keyboard types, screen orientations, and device sizes to ensure consistent behavior.
Document custom configurations
Document custom configurations
Add comments explaining why you’ve customized specific views or view controllers.
Next Steps
Toolbar Management
Add navigation buttons to the keyboard toolbar
Appearance Customization
Customize keyboard appearance (light/dark)
Debugging
Enable debug logging and troubleshooting
API Reference
Full API documentation