1.注册/移除通知
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector:
NotificationCenter.default.addObserver(self, selector:
}
deinit {
NotificationCenter.default.removeObserver(self)
}
2.实现通知中的方法
func keyBoardWillShow(_ notification: Notification){
let kbInfo = notification.userInfo
let kbRect = (kbInfo?[UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
let changeY = kbRect.origin.y - SCREEN_HEIGHT
let duration = kbInfo?[UIKeyboardAnimationDurationUserInfoKey] as! Double
UIView.animate(withDuration: duration) {
self.aTableView.transform = CGAffineTransform(translationX: 0, y: changeY)
}
}
func keyBoardWillHide(_ notification: Notification){
let kbInfo = notification.userInfo
let kbRect = (kbInfo?[UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
let changeY = kbRect.origin.y
let duration = kbInfo?[UIKeyboardAnimationDurationUserInfoKey] as! Double
UIView.animate(withDuration: duration) {
self.tableViewConstraint.update(inset: 0)
}
}