Created
September 14, 2016 13:33
-
-
Save desmondmc/050798e934358fc7f8b17a890f60276f to your computer and use it in GitHub Desktop.
RxSwift wrapper for 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
// | |
// 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