Last active
June 19, 2017 17:01
-
-
Save mkj-is/4b27e5a78adf5ddfdf3ba72bdbac40f7 to your computer and use it in GitHub Desktop.
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
// | |
// 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