Skip to content

Commit 81bfeb8

Browse files
committed
Add willShowVisibleHeight property
1 parent 3e7074d commit 81bfeb8

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

Sources/RxKeyboard.swift

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ public class RxKeyboard: NSObject {
2626
/// or `0` if the keyboard is not visible.
2727
public let visibleHeight: Driver<CGFloat>
2828

29+
/// Same with `visibleHeight` but only emits values when keyboard is about to show. This is
30+
/// useful when adjusting scroll view content offset.
31+
public let willShowVisibleHeight: Driver<CGFloat>
32+
2933

3034
// MARK: Private
3135

@@ -45,6 +49,13 @@ public class RxKeyboard: NSObject {
4549
let frameVariable = Variable<CGRect>(defaultFrame)
4650
self.frame = frameVariable.asDriver()
4751
self.visibleHeight = self.frame.map { UIScreen.main.bounds.height - $0.origin.y }
52+
self.willShowVisibleHeight = NotificationCenter.default.rx.notification(.UIKeyboardWillShow)
53+
.map { notification -> CGFloat in
54+
let rectValue = notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue
55+
let keyboardFrame = rectValue?.cgRectValue ?? defaultFrame
56+
return UIScreen.main.bounds.height - keyboardFrame.origin.y
57+
}
58+
.asDriver { _ in .empty() }
4859

4960
super.init()
5061

@@ -79,7 +90,7 @@ public class RxKeyboard: NSObject {
7990
}
8091

8192
// pan gesture
82-
let didPan = self.panRecognizer.rx.event
93+
let didPan = self.panRecognizer.rx.event.debug("pan")
8394
.withLatestFrom(frameVariable.asObservable()) { ($0, $1) }
8495
.flatMap { (gestureRecognizer, frame) -> Observable<CGRect> in
8596
guard let window = UIApplication.shared.windows.first,

0 commit comments

Comments
 (0)