Forked from laurilehmijoki/keyboardHeightObservable.swift
Created
May 9, 2021 21:44
-
-
Save SouzaRodrigo61/07bbf90707192e7220363f1f8e3216ac to your computer and use it in GitHub Desktop.
RxSwift Observable on iOS keyboard height
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import RxSwift // Version 3.2.0 | |
import RxCocoa // Version 3.2.0 | |
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() | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import RxSwift // Version 3.2.0 | |
import RxCocoa // Version 3.2.0 | |
import UIKit | |
class MyView: UIView { | |
fileprivate disposeBag = DisposeBag() | |
init() { | |
super.init(frame: CGRect.zero) | |
keyboardHeight() | |
.observeOn(MainScheduler.instance) | |
.subscribe(onNext: { (keyboardHeight): CGFloat in | |
// adjust other views with keyboardHeight | |
}) | |
.addDisposableTo(disposeBag) | |
} | |
required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment