Skip to content

Instantly share code, notes, and snippets.

@mkj-is
Last active June 19, 2017 17:01
Show Gist options
  • Save mkj-is/4b27e5a78adf5ddfdf3ba72bdbac40f7 to your computer and use it in GitHub Desktop.
Save mkj-is/4b27e5a78adf5ddfdf3ba72bdbac40f7 to your computer and use it in GitHub Desktop.
//
// Keyboardable.swift
//
// Created by Matěj Jirásek on 18/10/2016.
// Copyright © 2016 Matěj K. Jirásek. All rights reserved.
//
import Foundation
protocol Keyboardable {
func keyboardChanges(height: CGFloat)
}
extension Keyboardable {
func useKeyboard() {
let center = NotificationCenter.default
center.addObserver(forName: .UIKeyboardWillChangeFrame, object: nil, queue: nil, using: keyboardWillChange)
center.addObserver(forName: .UIKeyboardWillHide, object: nil, queue: nil, using: keyboardWillHide)
}
func keyboardWillChange(notification: Notification) {
guard let rect = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue else {
return
}
keyboardChanges(height: rect.size.height)
}
func keyboardWillHide(notification: Notification) {
keyboardChanges(height: 0)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment