@@ -19,6 +19,17 @@ public class RxKeyboard: NSObject {
19
19
/// Get a singleton instance.
20
20
public static let instance = RxKeyboard ( )
21
21
22
+ /// A view that receives pan gesture. The default value is the AppDelegate's window. Set this
23
+ /// value with the root view controller's view if the target is an App Extension.
24
+ public var gestureView : UIView ? {
25
+ get { return self . _gestureView ?? self . application? . delegate? . window ?? nil }
26
+ set {
27
+ self . _gestureView = newValue
28
+ self . panRecognizer. view? . removeGestureRecognizer ( self . panRecognizer)
29
+ newValue? . addGestureRecognizer ( self . panRecognizer)
30
+ }
31
+ }
32
+
22
33
/// An observable keyboard frame.
23
34
public let frame : Driver < CGRect >
24
35
@@ -35,13 +46,12 @@ public class RxKeyboard: NSObject {
35
46
36
47
fileprivate let disposeBag = DisposeBag ( )
37
48
fileprivate let panRecognizer = UIPanGestureRecognizer ( )
49
+
38
50
fileprivate let application : UIApplication ? = {
39
51
let selector = NSSelectorFromString ( " sharedApplication " )
40
52
return UIApplication . perform ( selector) ? . takeRetainedValue ( ) as? UIApplication
41
53
} ( )
42
- fileprivate var gestureView : UIView ? {
43
- return self . application? . delegate? . window ?? nil
44
- }
54
+ fileprivate var _gestureView : UIView ?
45
55
46
56
47
57
// MARK: Initializing
@@ -119,7 +129,8 @@ public class RxKeyboard: NSObject {
119
129
NotificationCenter . default. rx. notification ( . UIApplicationDidFinishLaunching)
120
130
. map { _ in Void ( ) }
121
131
. startWith ( Void ( ) ) // when RxKeyboard is initialized before UIApplication.window is created
122
- . subscribe ( onNext: { _ in
132
+ . subscribe ( onNext: { [ weak self] _ in
133
+ guard let `self` = self else { return }
123
134
self . gestureView? . addGestureRecognizer ( self . panRecognizer)
124
135
} )
125
136
. addDisposableTo ( self . disposeBag)
0 commit comments