WillAppear UIKeyboardWillShow UIKeyboardWillHide:
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name:NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name:NSNotification.Name.UIKeyboardWillHide, object: nil)
, View
func keyboardWillShow(notification: Notification) {
guard let keyboardFrame = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue else { return }
let convertedFrame = view.convert(keyboardFrame, from: nil)
tableView.contentInset.bottom = convertedFrame.height
}
keyboardWillHide 0 View . , .
func keyboardWillHide(notification: Notification) {
tableView.contentInset.bottom = 0
tableView.scrollToRow(at: IndexPath(row: 0, section: 0), at: .top, animated: true)
}
, textField , .
, - , , , , viewWillDisappear deinit .
deinit {
NotificationCenter.default.removeObserver(self, name: Notification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.removeObserver(self, name: Notification.Name.UIKeyboardWillHide, object: nil)
}