键盘隐藏时的iOS事件

Jau*_*ume 24 iphone xcode cocoa-touch objective-c ios

在键盘显示并按下完成按钮后,我需要控制键盘隐藏时.在iOS上隐藏键盘时会触发哪个事件?谢谢

Oma*_*ith 58

是使用以下内容

//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];
Run Code Online (Sandbox Code Playgroud)

而且 onKeyboardHide

-(void)onKeyboardHide:(NSNotification *)notification
{
     //keyboard will hide
}
Run Code Online (Sandbox Code Playgroud)

  • 是的,正确的,请检查更新的答案,完全隐藏的通知使用`UIKeyboardDidHideNotification` (2认同)

Dom*_*ico 6

如果您想知道用户何时按下完成按钮,您必须采用该UITextFieldDelegate协议,然后在您的View控制器中实现此方法:

斯威夫特3:

func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    // this will hide the keyboard
    textField.resignFirstResponder()
    return true
}
Run Code Online (Sandbox Code Playgroud)

如果您想知道键盘显示或隐藏的时间,请使用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!")
}
Run Code Online (Sandbox Code Playgroud)


kev*_*boh 5

您可以收听a UIKeyboardWillHideNotification,只要键盘被解除就会发送.

  • 确切地说,在键盘被解除之前发送通知. (7认同)