My rough idea is as below steps:
1, adding an outlet of bottom constraint to the view that you want to scroll up when keyboard shows or hides.
@IBOutlet weak var bottomConstraint: NSLayoutConstraint!
2, setting up to get keyboard show or hide notifications
override func viewWillAppear(_ animated: Bool) {
NotificationCenter.default.addObserver(self, selector:#selector(keyboardWillShow(notification:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(notification:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}
override func viewWillDisappear(_ animated: Bool) {
NotificationCenter.default.removeObserver(self)
}
3, handling when notification fired.
func keyboardWillShow(notification:Notification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
self.bottomConstraint.constant = keyboardSize.height
}
}
func keyboardWillHide(notification:Notification) {
self.bottomConstraint.constant = 0
}
just that simple
No comments:
Post a Comment