Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keyboard height is incorrect when other app has keyboard left visible #70

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 43 additions & 18 deletions Sources/RxKeyboard/RxKeyboard.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,20 @@ public class RxKeyboard: NSObject, RxKeyboardType {

// MARK: Private

private let frameVariable = BehaviorRelay<CGRect>(value: CGRect(
x: 0,
y: UIScreen.main.bounds.height,
width: UIScreen.main.bounds.width,
height: 0
))
private let disposeBag = DisposeBag()
private var keyboardChangeDisposeBag = DisposeBag()
private let panRecognizer = UIPanGestureRecognizer()


// MARK: Initializing

override init() {
let defaultFrame = CGRect(
x: 0,
y: UIScreen.main.bounds.height,
width: UIScreen.main.bounds.width,
height: 0
)
let frameVariable = BehaviorRelay<CGRect>(value: defaultFrame)
self.frame = frameVariable.asDriver().distinctUntilChanged()
self.visibleHeight = self.frame.map { UIScreen.main.bounds.height - $0.origin.y }
self.willShowVisibleHeight = self.visibleHeight
Expand All @@ -69,6 +69,41 @@ public class RxKeyboard: NSObject, RxKeyboardType {
self.isHidden = self.visibleHeight.map({ $0 == 0.0 }).distinctUntilChanged()
super.init()

bindChanges()

// gesture recognizer
self.panRecognizer.delegate = self
NotificationCenter.default.rx.notification(.UIApplicationDidFinishLaunching)
.map { _ in Void() }
.startWith(Void()) // when RxKeyboard is initialized before UIApplication.window is created
.subscribe(onNext: { _ in
UIApplication.shared.windows.first?.addGestureRecognizer(self.panRecognizer)
})
.disposed(by: self.disposeBag)

NotificationCenter.default.rx.notification(.UIApplicationWillResignActive)
.map { _ in Void() }
.subscribe(onNext: { [weak self] _ in
self?.keyboardChangeDisposeBag = DisposeBag()
})
.disposed(by: self.disposeBag)

NotificationCenter.default.rx.notification(.UIApplicationDidBecomeActive)
.map { _ in Void() }
.subscribe(onNext: { [weak self] _ in
self?.bindChanges()
})
.disposed(by: self.disposeBag)
}

private func bindChanges() {
let defaultFrame = CGRect(
x: 0,
y: UIScreen.main.bounds.height,
width: UIScreen.main.bounds.width,
height: 0
)

// keyboard will change frame
let willChangeFrame = NotificationCenter.default.rx.notification(.UIKeyboardWillChangeFrame)
.map { notification -> CGRect in
Expand Down Expand Up @@ -116,17 +151,7 @@ public class RxKeyboard: NSObject, RxKeyboardType {
// merge into single sequence
Observable.of(didPan, willChangeFrame, willHide).merge()
.bind(to: frameVariable)
.disposed(by: self.disposeBag)

// gesture recognizer
self.panRecognizer.delegate = self
NotificationCenter.default.rx.notification(.UIApplicationDidFinishLaunching)
.map { _ in Void() }
.startWith(Void()) // when RxKeyboard is initialized before UIApplication.window is created
.subscribe(onNext: { _ in
UIApplication.shared.windows.first?.addGestureRecognizer(self.panRecognizer)
})
.disposed(by: self.disposeBag)
.disposed(by: keyboardChangeDisposeBag)
}

}
Expand Down