[英]iOS event when keyboard hides
在键盘显示并按下完成按钮后,我需要控制键盘隐藏时。 在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
}
如果你想知道用户何时按下Done按钮,你必须采用UITextFieldDelegate
协议,然后在你的View控制器中实现这个方法:
斯威夫特3:
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
// this will hide the keyboard
textField.resignFirstResponder()
return true
}
如果您只想知道键盘何时显示或隐藏,请使用Notification
:
斯威夫特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
,只要键盘被解除就会发送。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.