简体   繁体   English

键盘隐藏时的iOS事件

[英]iOS event when keyboard hides

I need to control, after keyboard is shown and done button is pressed, when keyboard hides. 在键盘显示并按下完成按钮后,我需要控制键盘隐藏时。 Which event is triggered when hides keyboard on iOS? 在iOS上隐藏键盘时会触发哪个事件? Thank you 谢谢

Yes Use the following 是使用以下内容

//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];

And the onKeyboardHide onKeyboardHide

-(void)onKeyboardHide:(NSNotification *)notification
{
     //keyboard will hide
}

If you want to know when the user press the Done button, you have to adopt the UITextFieldDelegate protocol, then in you View controller implement this method: 如果你想知道用户何时按下Done按钮,你必须采用UITextFieldDelegate协议,然后在你的View控制器中实现这个方法:

Swift 3: 斯威夫特3:

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

If you want to know simply when keyboard is shown or is hiding, use a Notification : 如果您只想知道键盘何时显示或隐藏,请使用Notification

Swift 3: 斯威夫特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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM