@@ -35,6 +35,13 @@ public class RxKeyboard: NSObject {
3535
3636 fileprivate let disposeBag = DisposeBag ( )
3737 fileprivate let panRecognizer = UIPanGestureRecognizer ( )
38+ fileprivate let application : UIApplication ? = {
39+ let selector = NSSelectorFromString ( " sharedApplication " )
40+ return UIApplication . perform ( selector) ? . takeRetainedValue ( ) as? UIApplication
41+ } ( )
42+ fileprivate var gestureView : UIView ? {
43+ return self . application? . delegate? . window ?? nil
44+ }
3845
3946
4047 // MARK: Initializing
@@ -93,10 +100,10 @@ public class RxKeyboard: NSObject {
93100 . withLatestFrom ( frameVariable. asObservable ( ) ) { ( $0, $1) }
94101 . flatMap { ( gestureRecognizer, frame) -> Observable < CGRect > in
95102 guard case . changed = gestureRecognizer. state,
96- let window = UIApplication . shared . windows . first ,
103+ let view = gestureRecognizer . view ,
97104 frame. origin. y < UIScreen . main. bounds. height
98105 else { return . empty( ) }
99- let origin = gestureRecognizer. location ( in: window )
106+ let origin = gestureRecognizer. location ( in: view )
100107 var newFrame = frame
101108 newFrame. origin. y = max ( origin. y, UIScreen . main. bounds. height - frame. height)
102109 return . just( newFrame)
@@ -113,7 +120,7 @@ public class RxKeyboard: NSObject {
113120 . map { _ in Void ( ) }
114121 . startWith ( Void ( ) ) // when RxKeyboard is initialized before UIApplication.window is created
115122 . subscribe ( onNext: { _ in
116- UIApplication . shared . windows . first ? . addGestureRecognizer ( self . panRecognizer)
123+ self . gestureView ? . addGestureRecognizer ( self . panRecognizer)
117124 } )
118125 . addDisposableTo ( self . disposeBag)
119126 }
0 commit comments