Created
September 20, 2016 12:58
-
-
Save bfernandesbfs/4c3c0d34de535fecc7a9c13b8b683554 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
extension Keyboardable where Self: UIViewController { | |
public func addKeyboardObservers() { | |
NSNotificationCenter | |
.defaultCenter() | |
.addObserverForName(UIKeyboardWillShowNotification, | |
object: nil, | |
queue: nil) { [weak self] notification in | |
self?.keyboardWillShow(notification) | |
} | |
NSNotificationCenter | |
.defaultCenter() | |
.addObserverForName(UIKeyboardWillHideNotification, | |
object: nil, | |
queue: nil) { [weak self] notification in | |
self?.keyboardWillHide(notification) | |
} | |
} | |
public func removeKeyboardObservers() { | |
NSNotificationCenter | |
.defaultCenter() | |
.removeObserver(self, | |
name: UIKeyboardWillShowNotification, | |
object: nil) | |
NSNotificationCenter | |
.defaultCenter() | |
.removeObserver(self, | |
name: UIKeyboardWillHideNotification, | |
object: nil) | |
} | |
private func keyboardWillShow(notification: NSNotification) { | |
guard var info = getKeyboardInfo(notification) else { return } | |
if let tabBarHeight = tabBarController?.tabBar.frame.height { | |
info.height -= tabBarHeight | |
} | |
animateConstraints(info.height + addValueForKeyboard, duration: info.duration) | |
} | |
private func keyboardWillHide(notification: NSNotification) { | |
guard let info = getKeyboardInfo(notification) else { return } | |
animateConstraints(0, duration: info.duration) | |
} | |
private func getKeyboardInfo(notification: NSNotification) -> KeyboardHeightDuration? { | |
guard let userInfo = notification.userInfo else { return nil } | |
guard let rect = userInfo[UIKeyboardFrameEndUserInfoKey]?.CGRectValue() else { return nil } | |
guard let duration = userInfo[UIKeyboardAnimationDurationUserInfoKey]?.doubleValue else { return nil } | |
return (rect.height, duration) | |
} | |
private func animateConstraints(constant: CGFloat, duration: Double) { | |
layoutConstraintsForKeyboard.forEach { c in | |
c.constant = constant | |
} | |
UIView.animateWithDuration(duration) { | |
self.view.layoutIfNeeded() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment