Skip to content

Commit a348a3f

Browse files
committed
Add gesture recognizer to AppDelegate.window
1 parent 0ab3f8d commit a348a3f

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

Sources/RxKeyboard.swift

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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
.disposed(by: self.disposeBag)
119126
}

0 commit comments

Comments
 (0)