Skip to content

Instantly share code, notes, and snippets.

@iamjason
Created May 24, 2017 04:18
Show Gist options
  • Save iamjason/078dc84f7247ec70cefed3d94c194164 to your computer and use it in GitHub Desktop.
Save iamjason/078dc84f7247ec70cefed3d94c194164 to your computer and use it in GitHub Desktop.
RxSwift Helpers
import UIKit
import RxCocoa
import RxSwift
// KEYBOARD STUFF
extension UIViewController {
func setupViewResizerOnKeyboardShown(bag:DisposeBag) {
NotificationCenter.default.rx
.notification(Notification.Name.UIKeyboardWillShow)
.bind { [weak self] (notification) in
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue,
let v = self?.view,
let window = v.window?.frame {
v.frame = CGRect(x: v.frame.origin.x,
y: v.frame.origin.y,
width: v.frame.width,
height: window.origin.y + window.height - keyboardSize.height)
}
}.addDisposableTo(bag)
NotificationCenter.default.rx
.notification(Notification.Name.UIKeyboardWillHide)
.bind { [weak self] (notification) in
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue,
let v = self?.view {
let viewHeight = v.frame.height
v.frame = CGRect(x: v.frame.origin.x,
y: v.frame.origin.y,
width: v.frame.width,
height: viewHeight + keyboardSize.height)
}
}.addDisposableTo(bag)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment