Overview
IQKeyboardAppearanceManager is an internal class responsible for monitoring text input view events and applying keyboard appearance settings based on the configuration provided by IQKeyboardAppearanceConfiguration. This class automatically observes when text fields and text views begin editing and applies the configured appearance.
This is an internal class and is not meant to be instantiated or used directly. It is managed by
IQKeyboardManager.Class Declaration
Properties
The observer that monitors text input view events (begin editing, end editing) throughout the application.
The configuration object that contains the appearance settings to apply. This determines whether to override appearances and which appearance style to use.
Internal Methods
addTextInputViewObserver()
Registers an observer to monitor text input view events and apply keyboard appearance settings.- Subscribes to text input view notifications with identifier “IQKeyboardAppearanceManager”
- Monitors
.beginEditingevents - When a text field begins editing:
- Checks if
overrideAppearanceis enabled - Verifies the current appearance differs from the configured appearance
- Applies the configured appearance
- Reloads the input views to reflect the change
- Checks if
removeTextInputViewObserver()
Unsubscribes from text input view notifications and stops monitoring appearance changes.- Unsubscribes using the identifier “IQKeyboardAppearanceManager”
- Called when the manager is being deactivated or deallocated
How It Works
The appearance manager follows this workflow:- Initialization: When created, it automatically calls
addTextInputViewObserver()to start monitoring text input views - Event Monitoring: Uses
IQTextInputViewNotificationto observe when any text field or text view begins editing - Appearance Check: When
.beginEditingevent occurs:- Checks if
keyboardConfiguration.overrideAppearanceistrue - Compares current text input view’s
keyboardAppearancewith configuredappearance
- Checks if
- Appearance Application: If override is enabled and appearances differ:
- Sets
textInputView.keyboardAppearanceto the configured value - Calls
textInputView.reloadInputViews()to apply the change
- Sets
- No Action on End Editing: The
.endEditingevent is monitored but no action is taken
Code Example
Internal Implementation Flow
Architecture Notes
The manager uses a weak reference to self in the change handler to prevent retain cycles and ensure proper memory management.
The appearance is only applied during
.beginEditing events. This ensures the keyboard appearance is updated before it becomes visible to the user.The manager checks if the appearance is already correct before applying changes, avoiding unnecessary
reloadInputViews() calls which can cause visual glitches.Thread Safety
This class is marked with
@MainActor, ensuring all operations occur on the main thread. This is critical since it interacts with UIKit components.Availability
This class is unavailable in iOS App Extensions due to its dependency on UIApplication features and the notification system.
See Also
- IQKeyboardAppearanceConfiguration - The configuration object this manager uses
- IQKeyboardManager - The main keyboard manager that owns this component
- IQTextInputViewNotification - The notification system used for monitoring