如何关闭 iOS 键盘?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6906246/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I dismiss the iOS keyboard?
提问by Tyler McMaster
I have a UITextfield that i'd like to dismiss the keyboard for. I can't seem to make the keyboard go away no matter what code i use.
我有一个 UITextfield,我想为其关闭键盘。无论我使用什么代码,我似乎都无法让键盘消失。
采纳答案by Evan Mulawski
回答by benzado
If you have multiple text fields and don't know which one is first responder (or you simply don't have access to the text fields from wherever you are writing this code) you can call endEditing:
on the parent view containing the text fields.
如果您有多个文本字段并且不知道哪个是第一响应者(或者您根本无法从编写此代码的任何地方访问文本字段),您可以调用endEditing:
包含文本字段的父视图。
In a view controller's method, it would look like this:
在视图控制器的方法中,它看起来像这样:
[self.view endEditing:YES];
The parameter forces the text field to resign first responder status. If you were using a delegate to perform validation and wanted to stop everything until the text field's contents were valid, you could also code it like this:
该参数强制文本字段退出第一响应者状态。如果您使用委托来执行验证并希望在文本字段的内容有效之前停止一切,您也可以这样编码:
BOOL didEndEditing = [self.view endEditing:NO];
if (didEndEditing) {
// on to the next thing...
} else {
// text field must have said to first responder status: "never wanna give you up, never wanna let you down"
}
The endEditing:
method is much better than telling individual text fields to resignFirstResponder
, but for some reason I never even found out about it until recently.
该endEditing:
方法比将单个文本字段告诉 好得多resignFirstResponder
,但出于某种原因,直到最近我才发现它。
回答by user2216794
There are cases where no text field is the first responderbut the keyboard is on screen. In these cases, the above methods fail to dismiss the keyboard.
在某些情况下,没有文本字段是第一响应者,但键盘在屏幕上。在这些情况下,上述方法无法关闭键盘。
One example of how to get there:
如何到达那里的一个例子:
- push the ABPersonViewController on screen programmatically; open any contact;
- touch the "note" field (which becomes first responder and fires up the keyboard);
- swipe left on any other field to make the "delete" button appear;
- by this point you have no first responder among the text fields (just check programmatically) but the keyboard is still there. Calling [view endEditing:YES] does nothing.
- 以编程方式在屏幕上推送 ABPersonViewController;打开任何联系人;
- 触摸“注释”字段(它成为第一响应者并启动键盘);
- 在任何其他字段上向左滑动以显示“删除”按钮;
- 此时,您在文本字段中没有第一响应者(只需以编程方式检查),但键盘仍在那里。调用 [view endEditing:YES] 什么也不做。
In this case you also need to ask the view controller to exit the editing mode:
在这种情况下,您还需要要求视图控制器退出编辑模式:
[viewController setEditing:NO animated:YES];
回答by Andres Canella
I've discovered a case where endEditing
and resignFirstResponder
fail. This has worked for me in those cases.
我发现这里的情况endEditing
和resignFirstResponder
失败。在这些情况下,这对我有用。
ObjC
对象
[[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
[self setEditing:NO];
Swift
迅速
UIApplication.shared.sendAction(#selector(resignFirstResponder), to: nil, from: nil, for: nil)
回答by ibjazz
I suggest you add and action on your header file:
我建议您在头文件上添加和操作:
-(IBAction)removeKeyboard;
And in the implementation, write something like this:
在实现中,写这样的东西:
-(IBAction)removeKeyboard
{
[self.textfield resignFirstResponder];
}
In the NIB file, connect from the UITextFiled to the File's Owner on the option DidEndOnExit. That way, when you press return, the keyboard will disappear.
在 NIB 文件中,通过选项 DidEndOnExit 从 UITextFiled 连接到文件的所有者。这样,当您按回车键时,键盘就会消失。
Hope it helps!
希望能帮助到你!
回答by Thomas.Benz
In your view controller YourViewController.h file, make sure you implement UITextFieldDelegate protocol :
在你的视图控制器 YourViewController.h 文件中,确保你实现了 UITextFieldDelegate 协议:
@interface YourViewController : <UITextFieldDelegate>
@end
Then, in YourViewController.m file, implement the following instance method:
然后,在 YourViewController.m 文件中,实现以下实例方法:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[self.yourTextField1 resignFirstResponder];
[self.yourTextField2 resignFirstResponder];
...
[self.yourTextFieldn resignFirstResponder];
}
回答by ochimasu
as a last resort
作为最后的手段
let dummyTextView = UITextView(frame: .zero)
view.addSubview(dummyTextView)
dummyTextView.becomeFirstResponder()
dummyTextView.resignFirstResponder()
dummyTextView.removeFromSuperview()
回答by Milap Kundalia
This will resign one particular text field
这将退出一个特定的文本字段
// Swift
TextField.resignFirstResponder()
// Objective C
[TextField resignFirstResponder];
To resign any text field use below code
要退出任何文本字段,请使用以下代码
// Swift
self.view!.endEditing(true)
// Objective C
[self.view endEditing:YES];
回答by C?ur
To resign any text field in the app
退出应用程序中的任何文本字段
UIApplication.shared.keyWindow?.endEditing(true)
This approach is clean and guarantied to work because the keyWindow is, by definition, the root view of all possible views displaying a keyboard (source):
这种方法很干净并且可以保证工作,因为 keyWindow 根据定义是显示键盘的所有可能视图的根视图(source):
The key window receives keyboard and other non-touch related events. Only one window at a time may be the key window.
按键窗口接收键盘和其他非触摸相关的事件。一次只有一个窗口可能是关键窗口。
回答by BadPirate
If you don't know which textField is the first responder you can find it. I use this function:
如果您不知道哪个 textField 是第一响应者,您可以找到它。我使用这个功能:
UIView *resignFirstResponder(UIView *theView)
{
if([theView isFirstResponder])
{
[theView resignFirstResponder];
return theView;
}
for(UIView *subview in theView.subviews)
{
UIView *result = resignFirstResponder(subview);
if(result) return result;
}
return nil;
}
Then in your code call:
然后在您的代码调用中:
UIView *resigned = resignFirstResponder([UIScreen mainScreen]);