Skip to content

Instantly share code, notes, and snippets.

@vakhidbetrakhmadov
Last active August 19, 2019 10:09
Show Gist options
  • Save vakhidbetrakhmadov/070f6128558c6df8c767e84039bc4d9f to your computer and use it in GitHub Desktop.
Save vakhidbetrakhmadov/070f6128558c6df8c767e84039bc4d9f to your computer and use it in GitHub Desktop.
Utility protocol helping to simplify handling different keyboard notifications.
import UIKit
@objc protocol KeyboardObserving: class {
@objc optional func handleKeyboardWillShow(notification: Notification, keyboardSize: CGSize)
@objc optional func handleKeyboardWillHide(notification: Notification, keyboardSize: CGSize)
@objc optional func handleKeyboardWillChangeFrame(notification: Notification, keyboardSize: CGSize)
@objc optional func handleKeyboardDidChangeFrame(notification: Notification, keyboardSize: CGSize)
@objc optional func handleKeyboardDidShow(notification: Notification, keyboardSize: CGSize)
@objc optional func handleKeyboardDidHide(notification: Notification, keyboardSize: CGSize)
}
extension KeyboardObserving where Self: UIViewController {
func registerForKeyboardNotifications(from notificationCenter: NotificationCenter = .default) {
notificationCenter.addObserver(forName: UIResponder.keyboardWillShowNotification, object: nil, queue: nil) { [weak self] in
self?.handleKeyboardNotifications(notification: $0)
}
notificationCenter.addObserver(forName: UIResponder.keyboardWillHideNotification, object: nil, queue: nil) { [weak self] in
self?.handleKeyboardNotifications(notification: $0)
}
notificationCenter.addObserver(forName: UIResponder.keyboardWillChangeFrameNotification, object: nil, queue: nil) { [weak self] in
self?.handleKeyboardNotifications(notification: $0)
}
notificationCenter.addObserver(forName: UIResponder.keyboardDidChangeFrameNotification, object: nil, queue: nil) { [weak self] in
self?.handleKeyboardNotifications(notification: $0)
}
notificationCenter.addObserver(forName: UIResponder.keyboardDidShowNotification, object: nil, queue: nil) { [weak self] in
self?.handleKeyboardNotifications(notification: $0)
}
notificationCenter.addObserver(forName: UIResponder.keyboardDidHideNotification, object: nil, queue: nil) { [weak self] in
self?.handleKeyboardNotifications(notification: $0)
}
}
private func handleKeyboardNotifications(notification: Notification) {
guard let userInfo = notification.userInfo else { return }
let keyboardScreenEndFrame = (userInfo[UIResponder.keyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
let keyboardViewEndFrame = view.convert(keyboardScreenEndFrame, from: view.window)
let keyboardSize = keyboardViewEndFrame.size
switch notification.name {
case UIResponder.keyboardWillShowNotification:
handleKeyboardWillShow?(notification: notification, keyboardSize: keyboardSize)
case UIResponder.keyboardWillHideNotification:
handleKeyboardWillHide?(notification: notification, keyboardSize: keyboardSize)
case UIResponder.keyboardWillChangeFrameNotification:
handleKeyboardWillChangeFrame?(notification: notification, keyboardSize: keyboardSize)
case UIResponder.keyboardDidChangeFrameNotification:
handleKeyboardDidChangeFrame?(notification: notification, keyboardSize: keyboardSize)
case UIResponder.keyboardDidShowNotification:
handleKeyboardDidShow?(notification: notification, keyboardSize: keyboardSize)
case UIResponder.keyboardDidHideNotification:
handleKeyboardDidHide?(notification: notification, keyboardSize: keyboardSize)
default: break
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment