Skip to content

Instantly share code, notes, and snippets.

@desmondmc
Created September 14, 2016 13:33
Show Gist options
  • Save desmondmc/050798e934358fc7f8b17a890f60276f to your computer and use it in GitHub Desktop.
Save desmondmc/050798e934358fc7f8b17a890f60276f to your computer and use it in GitHub Desktop.
RxSwift wrapper for keyboard height.
//
// KeyboardHeight.swift
// bodytonic-ios
//
// Created by Desmond McNamee on 2016-08-28.
// Copyright © 2016 Stadium Studio. All rights reserved.
//
import UIKit
import RxSwift
class KeyboardHeight {
let currentKeyboardHeight = Variable(Float(0.0))
static let sharedInstance = KeyboardHeight()
private init() {
NSNotificationCenter.defaultCenter().addObserver(
self,
selector: #selector(KeyboardHeight.keyboardWillShow),
name: UIKeyboardWillShowNotification,
object: nil)
NSNotificationCenter.defaultCenter().addObserver(
self,
selector: #selector(KeyboardHeight.keyboardWillHide),
name: UIKeyboardWillHideNotification,
object: nil)
}
@objc func keyboardWillShow(notification: NSNotification) {
guard
let info = notification.userInfo,
let size = info[UIKeyboardFrameEndUserInfoKey] else { return }
let newHeight = size.CGRectValue().size.height
self.currentKeyboardHeight.value = Float(newHeight)
}
@objc func keyboardWillHide(notification: NSNotification) {
self.currentKeyboardHeight.value = 0.0
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment