Created
March 17, 2020 07:50
-
-
Save KaQuMiQ/305e2f5be32703f61f567eec17846100 to your computer and use it in GitHub Desktop.
Getting current first responder
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import UIKit | |
| extension UIResponder { | |
| private static weak var _currentFirstResponder: UIResponder? | |
| public static var currentFirstResponder: UIResponder? { | |
| dispatchPrecondition(condition: .onQueue(.main)) | |
| _currentFirstResponder = nil | |
| UIApplication.shared.sendAction(#selector(UIResponder.findFirstResponder(_:)), to: nil, from: nil, for: nil) | |
| return _currentFirstResponder | |
| } | |
| @objc private func findFirstResponder(_ sender: Any) { | |
| UIResponder._currentFirstResponder = self | |
| } | |
| public static var firstResponderExists: Bool { | |
| dispatchPrecondition(condition: .onQueue(.main)) | |
| return currentFirstResponder != nil | |
| } | |
| public static func resignCurrentFirstResponder() { | |
| dispatchPrecondition(condition: .onQueue(.main)) | |
| currentFirstResponder?.resignFirstResponder() | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment