Swift3.0キーボードのイジェクトと回収を監視
2559 ワード
1.登録/削除通知
2.通知の実現方法
override func viewDidLoad() {
super.viewDidLoad()
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)
}
deinit {
//
NotificationCenter.default.removeObserver(self)
}
2.通知の実現方法
//
func keyBoardWillShow(_ notification: Notification){
// userInfo
let kbInfo = notification.userInfo
// size
let kbRect = (kbInfo?[UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
// y
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)
}
}