我需要控制,在键盘显示和完成按钮被按下之后,当键盘隐藏时。在iOS上隐藏键盘时触发的事件是哪个?谢谢
可以,使用以下方法:
//UIKeyboardDidHideNotification when keyboard is fully hidden
//name:UIKeyboardWillHideNotification when keyboard is going to be hidden
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(onKeyboardHide:) name:UIKeyboardWillHideNotification object:nil];
而且是 onKeyboardHide
-(void)onKeyboardHide:(NSNotification *)notification
{
//keyboard will hide
}
UIKeyboardDidHideNotification
。 - Omar AbdelhafithUITextFieldDelegate
协议,然后在您的视图控制器中实现此方法:func textFieldShouldReturn(_ textField: UITextField) -> Bool {
// this will hide the keyboard
textField.resignFirstResponder()
return true
}
如果您想简单了解键盘何时显示或隐藏,请使用 Notification
:
Swift 3:
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillShow(_:)), name: .UIKeyboardWillShow , object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardWillHide(_:)), name: .UIKeyboardWillHide , object: nil)
func keyboardWillShow(_ notification: NSNotification) {
print("keyboard will show!")
// To obtain the size of the keyboard:
let keyboardSize:CGSize = (notification.userInfo![UIKeyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue.size
}
func keyboardWillHide(_ notification: NSNotification) {
print("Keyboard will hide!")
}
UIKeyboardWillHideNotification
,每当键盘被隐藏时它就会被发送。