Skip to content

Instantly share code, notes, and snippets.

@blazovics
Last active October 13, 2021 21:00
Show Gist options
  • Save blazovics/f38dbea158868b2671dcf5628f10b0d5 to your computer and use it in GitHub Desktop.
Save blazovics/f38dbea158868b2671dcf5628f10b0d5 to your computer and use it in GitHub Desktop.
@objc private func keyboardWillShow(notification: Notification) {
if let userInfo = notification.userInfo, let keyboardSize = (userInfo[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
if passwordTextField.frame.maxY > (view.frame.height - keyboardSize.height) {
imageViewTopConstraint.constant = -1 * (passwordTextField.frame.maxY - (view.frame.height - keyboardSize.height))
}
}
}
@objc private func keyboardWillHide(notification: Notification) {
imageViewTopConstraint.constant = 0
}
@objc private func keyboardWillShow(notification: Notification) {
if let userInfo = notification.userInfo,
let keyboardSize = (userInfo[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue,
let duration = (userInfo[UIResponder.keyboardAnimationDurationUserInfoKey] as? NSNumber)?.doubleValue {
if passwordTextField.frame.maxY > (view.frame.height - keyboardSize.height) {
UIView.animate(withDuration: duration, animations: {
self.imageViewTopConstraint.constant = -1 * (self.passwordTextField.frame.maxY - (self.view.frame.height - keyboardSize.height))
self.view.layoutIfNeeded()
})
}
}
}
@objc private func keyboardWillHide(notification: Notification) {
if let userInfo = notification.userInfo,
let duration = (userInfo[UIResponder.keyboardAnimationDurationUserInfoKey] as? NSNumber)?.doubleValue {
UIView.animate(withDuration: duration) {
self.imageViewTopConstraint.constant = 0
self.view.layoutIfNeeded()
}
}
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
NotificationCenter.default.addObserver(self, selector: #selector(LoginViewController.keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(LoginViewController.keyboardWillHide), name: UIResponder.keyboardWillHideNotification, object: nil)
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
NotificationCenter.default.removeObserver(self)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment