@@ -35,6 +35,13 @@ public class RxKeyboard: NSObject {
35
35
36
36
fileprivate let disposeBag = DisposeBag ( )
37
37
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
+ }
38
45
39
46
40
47
// MARK: Initializing
@@ -93,10 +100,10 @@ public class RxKeyboard: NSObject {
93
100
. withLatestFrom ( frameVariable. asObservable ( ) ) { ( $0, $1) }
94
101
. flatMap { ( gestureRecognizer, frame) -> Observable < CGRect > in
95
102
guard case . changed = gestureRecognizer. state,
96
- let window = UIApplication . shared . windows . first ,
103
+ let view = gestureRecognizer . view ,
97
104
frame. origin. y < UIScreen . main. bounds. height
98
105
else { return . empty( ) }
99
- let origin = gestureRecognizer. location ( in: window )
106
+ let origin = gestureRecognizer. location ( in: view )
100
107
var newFrame = frame
101
108
newFrame. origin. y = max ( origin. y, UIScreen . main. bounds. height - frame. height)
102
109
return . just( newFrame)
@@ -113,7 +120,7 @@ public class RxKeyboard: NSObject {
113
120
. map { _ in Void ( ) }
114
121
. startWith ( Void ( ) ) // when RxKeyboard is initialized before UIApplication.window is created
115
122
. subscribe ( onNext: { _ in
116
- UIApplication . shared . windows . first ? . addGestureRecognizer ( self . panRecognizer)
123
+ self . gestureView ? . addGestureRecognizer ( self . panRecognizer)
117
124
} )
118
125
. disposed ( by: self . disposeBag)
119
126
}
0 commit comments