Overview
IQScrollViewConfiguration is an internal struct that captures the initial state of a scroll view before keyboard adjustments are applied. It records content offsets, insets, and scroll indicators, then provides restoration functionality to return the scroll view to its original state when the keyboard is dismissed.
Purpose
The scroll view configuration:- Captures the initial content offset before keyboard-induced scrolling
- Records the original content insets
- Stores the initial scroll indicator insets
- Tracks whether content offset restoration is allowed
- Provides restoration functionality to revert scroll adjustments
- Handles special cases for UITableView, UICollectionView, and UIStackView
Properties
scrollView
A reference to the scroll view whose state is being tracked. This can be any
UIScrollView subclass, including UITableView, UICollectionView, and UITextView.startingContentOffset
The original content offset of the scroll view before any keyboard adjustments.Captured during initialization from:This value is used to restore the scroll position when the keyboard is dismissed, ensuring users return to their original scroll location.
startingScrollIndicatorInsets
The original scroll indicator insets before adjustments.Captured during initialization from:These insets control the positioning of the scroll bar and are adjusted to avoid overlapping with the keyboard.
startingContentInset
The original content insets before keyboard adjustments.Captured during initialization from:Content insets are modified to create space for the keyboard, and this property stores the original values for restoration.
canRestoreContentOffset
An internal flag indicating whether content offset restoration is permitted for this scroll view.This is set during initialization based on the keyboard manager’s configuration and the specific scroll view’s settings. When
false, only content insets are restored, not the scroll position.hasChanged
Indicates whether the scroll view’s state has been modified from its initial configuration.Returns
true when:- The current content inset differs from
startingContentInset, OR - Content offset restoration is enabled and the current content offset differs from
startingContentOffset
Initialization
scrollView: The scroll view to trackcanRestoreContentOffset: Whether to restore content offset on dismissal
- Stores a reference to the scroll view
- Captures
contentOffset→startingContentOffset - Captures
contentInset→startingContentInset - Captures
verticalScrollIndicatorInsets→startingScrollIndicatorInsets - Stores the
canRestoreContentOffsetflag
Methods
restore(for:)
Restores the scroll view to its original state, optionally considering the active text input view.textInputView: The currently active text input view, used to determine animation behavior
Returns
true if any restoration occurred (content inset or content offset was changed), false if the scroll view was already in its original state.Restore Scroll Indicators
If the current scroll indicator insets differ from
startingScrollIndicatorInsets:Animation Behavior
The restoration method includes special logic for animated vs. non-animated content offset restoration:UITableView
Animated restoration to provide smooth scroll-back behavior
UICollectionView
Animated restoration to sync with layout updates
UIStackView Container
Animated when text input is inside a stack view (Bug fixes: #1365, #1508, #1541)
For other scroll views (including plain
UIScrollView and UITextView), content offset is set directly without animation to avoid visual glitches.Special Handling
UIStackView Detection
When restoring content offset, the configuration checks if the text input view is inside aUIStackView:
Per-View Restoration Control
The restoration respects the per-viewrestoreContentOffset setting:
Layout Synchronization
After modifying content insets, the configuration forces a layout pass:Lifecycle
When Configuration is Created
A newIQScrollViewConfiguration is created when:
- A text input view inside a scroll view becomes active
- The keyboard appears and scroll view adjustments are needed
- The library determines the scroll view needs to be scrolled or inset-adjusted to reveal the text input
When Configuration is Restored
Therestore(for:) method is called when:
- The keyboard is dismissed
- A text input inside the scroll view resigns first responder
- The user navigates away from the view controller
- The keyboard manager is disabled
- An orientation change requires restoration before recalculation
Usage in IQKeyboardManager
The keyboard manager maintains a collection of scroll view configurations:Value Type Benefits
Using a struct (value type) provides several advantages:- State Immutability: Original values (
startingContentOffset, etc.) cannot be accidentally modified - Clear Snapshots: Each configuration represents a point-in-time snapshot of the scroll view state
- Simplified Lifecycle: No memory management concerns or cleanup required
- Thread Safety: Value semantics prevent unintended sharing across contexts
Bug Fixes Referenced
The implementation includes fixes for several specific issues:- #1365, #1508, #1541: Stack view layout issues requiring animated restoration
- #1901, #1996: Collection view and table view restoration glitches
- #1996: Content inset restoration requiring explicit
layoutIfNeeded()
Thread Safety
The struct is marked with
@MainActor, ensuring all operations occur on the main thread. This is essential since it manipulates scroll view properties that affect UI layout.See Also
- IQActiveConfiguration - Coordinates overall keyboard adjustment state
- IQRootControllerConfiguration - Tracks root view controller state
- IQKeyboardManager - Main keyboard manager class
- UIScrollView Extensions - Per-scroll-view customization APIs