Iphone 键盘隐藏时的iOS事件
我需要控制,在键盘显示并按下“完成”按钮后,当键盘隐藏时。在iOS上隐藏键盘时触发哪个事件?谢谢您可以收听Iphone 键盘隐藏时的iOS事件,iphone,objective-c,ios,xcode,cocoa-touch,Iphone,Objective C,Ios,Xcode,Cocoa Touch,我需要控制,在键盘显示并按下“完成”按钮后,当键盘隐藏时。在iOS上隐藏键盘时触发哪个事件?谢谢您可以收听UIKeyboardWillHideNotification,它会在键盘关闭时发送。是使用以下命令 //UIKeyboardDidHideNotification when keyboard is fully hidden //name:UIKeyboardWillHideNotification when keyboard is going to be hidden [[NSNotifi
UIKeyboardWillHideNotification
,它会在键盘关闭时发送。是使用以下命令
//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
}
如果您想知道用户何时按下“完成”按钮,则必须采用
UITextFieldDelegate
协议,然后在您的视图控制器中实现此方法:
Swift 3:
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
// this will hide the keyboard
textField.resignFirstResponder()
return true
}
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!")
}
如果您只想知道键盘何时显示或隐藏,请使用通知
:
Swift 3:
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
// this will hide the keyboard
textField.resignFirstResponder()
return true
}
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!")
}
准确地说,通知是在键盘被解除之前发送的。可能重复的通知将在解除时触发,而不是在键盘完全隐藏时触发。是的,正确,请检查更新的答案,以获取完全隐藏的通知使用
uikeyboarddidenotification