Skip to content

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Sep 19, 2025

The Done button in IQKeyboardManager's toolbar was returning incorrect values for UITraitCollection.current.userInterfaceStyle in delegate methods like textFieldDidEndEditing, while the Return key worked correctly.

Root Cause

The issue occurred because the Done button action calls resignFirstResponder() before invoking delegate methods, which changes the responder chain context and causes UITraitCollection.current to lose the proper view controller context.

Solution

Objective-C Version (Automatic Fix)

Modified the doneAction: method in IQKeyboardManager.m to:

  • Capture the text field's view controller before resigning first responder
  • Use performAsCurrentTraitCollection: to preserve trait collection context during delegate invocation

Swift Version (Developer Utilities)

Added IQKeyboardToolbarManager+TraitCollectionFix.swift with:

  • performWithCurrentTraitCollection utility method for developers
  • Clear documentation and usage examples

Usage

Objective-C: No changes required - fix works automatically.

Swift: Wrap trait collection access in delegate methods:

func textFieldDidEndEditing(_ textField: UITextField) {
    self.performWithCurrentTraitCollection {
        let isDark = UITraitCollection.current.userInterfaceStyle == .dark
        // Now returns correct value regardless of Done vs Return button
    }
}

Testing

To verify the fix:

  1. Set device to dark/light mode
  2. Tap text field to show keyboard with toolbar
  3. Press Done button and check userInterfaceStyle value
  4. Press Return key and compare values
  5. Both should return the same correct value

The implementation ensures backward compatibility while providing a reliable solution for accessing trait collection information in keyboard delegate methods.

Fixes #2099.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Copilot AI and others added 2 commits September 19, 2025 09:40
Co-authored-by: hackiftekhar <3831495+hackiftekhar@users.noreply.github.com>
Co-authored-by: hackiftekhar <3831495+hackiftekhar@users.noreply.github.com>
@Copilot Copilot AI changed the title [WIP] Wrong 'UITraitCollection.current.userInterfaceStyle' on pressing 'Done' button Fix UITraitCollection.current.userInterfaceStyle returning incorrect values on Done button press Sep 19, 2025
@Copilot Copilot AI requested a review from hackiftekhar September 19, 2025 09:46
Copilot finished work on behalf of hackiftekhar September 19, 2025 09:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Wrong 'UITraitCollection.current.userInterfaceStyle' on pressing 'Done' button
2 participants