Skip to content

Instantly share code, notes, and snippets.

@Anatoli-Petrosyants
Last active November 28, 2018 09:33
Show Gist options
  • Save Anatoli-Petrosyants/9a91504fbcb66829068ac45d7e71b9af to your computer and use it in GitHub Desktop.
Save Anatoli-Petrosyants/9a91504fbcb66829068ac45d7e71b9af to your computer and use it in GitHub Desktop.
import RxSwift
import RxCocoa
func keyboardHeight() -> Observable<CGFloat> {
return Observable
.from([
NotificationCenter.default.rx.notification(NSNotification.Name.UIKeyboardWillShow)
.map { notification -> CGFloat in
(notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue.height ?? 0
},
NotificationCenter.default.rx.notification(NSNotification.Name.UIKeyboardWillHide)
.map { _ -> CGFloat in
0
}
])
.merge()
}
// usage
keyboardHeight()
.observeOn(MainScheduler.instance)
.subscribe(onNext: { (keyboardHeight): CGFloat in
// adjust other views with keyboardHeight
})
.addDisposableTo(disposeBag)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment