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) | |
} | |
} |
Seems to work for me...
Works fine for me, thanks!
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:
extension Reactive where Base: UIResponder {
var isFirstResponder: Observable<Bool> {
return observe(\.isFirstResponder)
.distinctUntilChanged()
}
}
However, UIKit's views do not properly support KVO for isFirstResponder
, so I had to re-enable it by overriding becomeFirstResponder
and resignFirstResponder
in every subclass that needs to be observed via rx.isFirstResponder
:
override func becomeFirstResponder() -> Bool {
willChangeValue(for: \.isFirstResponder)
let result = super.becomeFirstResponder()
didChangeValue(for: \.isFirstResponder)
return result
}
override func resignFirstResponder() -> Bool {
willChangeValue(for: \.isFirstResponder)
let result = super.resignFirstResponder()
didChangeValue(for: \.isFirstResponder)
return result
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I haven't used this in quite a while. It worked at the time. What does not work?