Created
October 5, 2018 02:53
-
-
Save Revolucent/416fb22b9c39e29ec35001378fc9752f to your computer and use it in GitHub Desktop.
isFirstResponder observable with RxSwift
This file contains 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 RxCocoa | |
import RxSwift | |
import UIKit | |
extension Reactive where Base: UIView { | |
var isFirstResponder: Observable<Bool> { | |
return Observable | |
.merge( | |
methodInvoked(#selector(UIView.becomeFirstResponder)), | |
methodInvoked(#selector(UIView.resignFirstResponder)) | |
) | |
.map{ [weak view = self.base] _ in | |
view?.isFirstResponder ?? false | |
} | |
.startWith(base.isFirstResponder) | |
.distinctUntilChanged() | |
.share(replay: 1) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This has stopped working in iOS 18, causing a stack overflow inside
-[UITextInputUIResponderAccessibility becomeFirstResponder]
I wish
isFirstResponder
was backed by KVO, we'd simply used this:However, UIKit's views do not properly support KVO for
isFirstResponder
, so I had to re-enable it by overridingbecomeFirstResponder
andresignFirstResponder
in every subclass that needs to be observed viarx.isFirstResponder
: