Last active
December 28, 2016 08:25
-
-
Save pangers/4a648033e0a110e1227ec7c6a7421816 to your computer and use it in GitHub Desktop.
This file contains 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
//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