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)
如果您想知道用户何时按下完成按钮,您必须采用该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)
| 归档时间: |
|
| 查看次数: |
24494 次 |
| 最近记录: |