@@ -47,10 +47,21 @@ public class RxKeyboard: NSObject, RxKeyboardType {
47
47
private let disposeBag = DisposeBag ( )
48
48
private let panRecognizer = UIPanGestureRecognizer ( )
49
49
50
-
51
50
// MARK: Initializing
52
51
53
52
override init ( ) {
53
+ #if swift(>=4.2)
54
+ let keyboardWillChangeFrame = UIResponder . keyboardWillChangeFrameNotification
55
+ let keyboardWillHide = UIResponder . keyboardWillHideNotification
56
+ let keyboardFrameEndKey = UIResponder . keyboardFrameEndUserInfoKey
57
+ let applicationDidFinishLaunching = UIApplication . didFinishLaunchingNotification
58
+ #else
59
+ let keyboardWillChangeFrame = NSNotification . Name. UIKeyboardWillChangeFrame
60
+ let keyboardWillHide = NSNotification . Name. UIKeyboardWillHide
61
+ let keyboardFrameEndKey = UIKeyboardFrameEndUserInfoKey
62
+ let applicationDidFinishLaunching = NSNotification . Name. UIApplicationDidFinishLaunching
63
+ #endif
64
+
54
65
let defaultFrame = CGRect (
55
66
x: 0 ,
56
67
y: UIScreen . main. bounds. height,
@@ -70,9 +81,9 @@ public class RxKeyboard: NSObject, RxKeyboardType {
70
81
super. init ( )
71
82
72
83
// keyboard will change frame
73
- let willChangeFrame = NotificationCenter . default. rx. notification ( . UIKeyboardWillChangeFrame )
84
+ let willChangeFrame = NotificationCenter . default. rx. notification ( keyboardWillChangeFrame )
74
85
. map { notification -> CGRect in
75
- let rectValue = notification. userInfo ? [ UIKeyboardFrameEndUserInfoKey ] as? NSValue
86
+ let rectValue = notification. userInfo ? [ keyboardFrameEndKey ] as? NSValue
76
87
return rectValue? . cgRectValue ?? defaultFrame
77
88
}
78
89
. map { frame -> CGRect in
@@ -85,9 +96,9 @@ public class RxKeyboard: NSObject, RxKeyboardType {
85
96
}
86
97
87
98
// keyboard will hide
88
- let willHide = NotificationCenter . default. rx. notification ( . UIKeyboardWillHide )
99
+ let willHide = NotificationCenter . default. rx. notification ( keyboardWillHide )
89
100
. map { notification -> CGRect in
90
- let rectValue = notification. userInfo ? [ UIKeyboardFrameEndUserInfoKey ] as? NSValue
101
+ let rectValue = notification. userInfo ? [ keyboardFrameEndKey ] as? NSValue
91
102
return rectValue? . cgRectValue ?? defaultFrame
92
103
}
93
104
. map { frame -> CGRect in
@@ -120,7 +131,7 @@ public class RxKeyboard: NSObject, RxKeyboardType {
120
131
121
132
// gesture recognizer
122
133
self . panRecognizer. delegate = self
123
- NotificationCenter . default. rx. notification ( . UIApplicationDidFinishLaunching )
134
+ NotificationCenter . default. rx. notification ( applicationDidFinishLaunching )
124
135
. map { _ in Void ( ) }
125
136
. startWith ( Void ( ) ) // when RxKeyboard is initialized before UIApplication.window is created
126
137
. subscribe ( onNext: { _ in
0 commit comments