Skip to content

Instantly share code, notes, and snippets.

@pangers
Last active December 28, 2016 08:25
Show Gist options
  • Save pangers/4a648033e0a110e1227ec7c6a7421816 to your computer and use it in GitHub Desktop.
Save pangers/4a648033e0a110e1227ec7c6a7421816 to your computer and use it in GitHub Desktop.
//MARK: - Public
extension KeyboardAvoidable where Self: UIViewController {
func setupKeyboardNotifications() {
NotificationCenter.default.addObserver(forName: .UIKeyboardWillShow, object: nil, queue: nil, using: {
[weak self] notification in
//Had to use block because protocols don't understand selectors
//Therefore whoever conforms to `KeyboardAvoidable` needs to call `removeKeyboardNotifications` in deinit
self?.keyboardWillAppear(notification)
})
NotificationCenter.default.addObserver(forName: .UIKeyboardWillHide, object: nil, queue: nil, using: {
[weak self] notification in
//Had to use block because protocols don't understand selectors
//Therefore whoever conforms to `KeyboardAvoidable` needs to call `removeKeyboardNotifications` in deinit
self?.keyboardWillDisappear(notification)
})
}
//Need to call this in deinit of ViewController
func removeKeyboardNotifications() {
NotificationCenter.default.removeObserver(self)
}
}
//MARK: - Private
fileprivate extension KeyboardAvoidable where Self: UIViewController {
func keyboardWillAppear(_ notification: Notification) {
}
func keyboardWillDisappear(_ notification: Notification) {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment