UIKeyboardWillShowNotification: String
UIKeyboardDidShowNotification: String
UIKeyboardWillHideNotification: String
UIKeyboardDidHideNotification: String
1. 注册
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillAppear:", name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillDisappear:", name: UIKeyboardWillHideNotification, object: nil)
2. 使用
func keyboardWillAppear(notification: NSNotification) {
// 获取键盘信息
let keyboardinfo = notification.userInfo![UIKeyboardFrameBeginUserInfoKey]
let keyboardheight: CGFloat = (keyboardinfo?.CGRectValue.size.height)!
textField.frame = CGRectMake(0, self.view.height - keyboardheight - 40, self.view.width, 40)
print("键盘弹起")
print(keyboardheight)
}
func keyboardWillDisappear(notification: NSNotification) {
print("键盘落下")
textField.frame = CGRectMake(0, self.view.height, self.view.width, 40)
}