-
键盘出现和消失获取键盘的相关信息
注册通知
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)
// 触发时间
func keyBoardWillShow(_ notification: Notification){
guard let frame = notification.userInfo?[UIKeyboardFrameEndUserInfoKey] else{
return
}
let y = (frame as! NSValue).cgRectValue.origin.y
}
提示:
notification.userInfo?[UIKeyboardFrameEndUserInfoKey] 获取的值为为NSValue 类型,所以需要稍微转一下