Last active
July 6, 2018 03:00
-
-
Save NeilsUltimateLab/fd669ebd6a4dd8ad9378856e6a176f53 to your computer and use it in GitHub Desktop.
Handle keyboard notification for custom transition. This implementation handles interaction with keyboard frame.
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
extension ViewController { | |
@objc func handleKeyboardWillShow(notification: Notification) { | |
guard notification.name == UIResponder.keyboardWillShowNotification else { | |
return | |
} | |
guard let keyboardFrame = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect else { return } | |
let viewFrame = self.view.frame | |
let newRect = self.view.convert(self.view.frame, to: self.view.window) | |
print("KeyboardFrame: \(keyboardFrame)") | |
print("ViewFrame: \(viewFrame)") | |
print("ConvertedFrame: \(newRect)") | |
guard newRect.intersects(keyboardFrame) else { | |
return | |
} | |
guard let windowHeight = self.view.window?.frame.height else { return } | |
UIView.animate(withDuration: 0.2) { | |
self.navigationController?.view.frame.origin.y = windowHeight - keyboardFrame.height - 4 - self.view.frame.height | |
} | |
} | |
@objc func handleKeyboardWillHide(notification: Notification) { | |
guard notification.name == UIResponder.keyboardWillHideNotification else { | |
return | |
} | |
guard let windowCenter = self.view.window?.center else { return } | |
UIView.animate(withDuration: 0.3) { | |
self.navigationController?.view.center.y = windowCenter.y | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment