如何关闭 iOS 键盘?
我有一个 UITextfield,我想关闭键盘。无论我使用什么代码,我似乎都无法让键盘消失。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(16)
如果您有多个文本字段并且不知道哪个是第一响应者(或者您根本无法从编写此代码的任何位置访问文本字段),您可以调用
endEditing:
包含文本字段的父视图。在视图控制器的方法中,它看起来像这样:
该参数强制文本字段放弃第一响应者状态。如果您使用委托来执行验证并希望停止所有操作,直到文本字段的内容有效,您也可以像这样编写代码:
endEditing:
方法比告诉各个文本字段要好得多resignFirstResponder
,但由于某种原因我直到最近才发现它。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.In a view controller's method, it would look like this:
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:
The
endEditing:
method is much better than telling individual text fields toresignFirstResponder
, but for some reason I never even found out about it until recently.此处,显示和隐藏键盘部分中的第二段。
Here, second paragraph in the Showing and Hiding the Keyboard section.
我发现
endEditing
和resignFirstResponder
失败的情况。在这些情况下,这对我有用。对象
Swift
I've discovered a case where
endEditing
andresignFirstResponder
fail. This has worked for me in those cases.ObjC
Swift
在某些情况下,没有文本字段是第一响应者,但键盘在屏幕上。
在这些情况下,上述方法无法关闭键盘。
如何实现这一点的一个示例:
在这种情况下,您还需要要求视图控制器退出编辑模式:
There are cases where no text field is the first responder but the keyboard is on screen.
In these cases, the above methods fail to dismiss the keyboard.
One example of how to get there:
In this case you also need to ask the view controller to exit the editing mode:
我建议您在头文件上添加操作:
在实现中,编写如下内容:
在 NIB 文件中,通过选项 DidEndOnExit 从 UITextFiled 连接到文件所有者。这样,当你按回车键时,键盘就会消失。
希望有帮助!
I suggest you add and action on your header file:
And in the implementation, write something like this:
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.
Hope it helps!
在视图控制器 YourViewController.h 文件中,确保实现 UITextFieldDelegate 协议:
然后,在 YourViewController.m 文件中,实现以下实例方法:
In your view controller YourViewController.h file, make sure you implement UITextFieldDelegate protocol :
Then, in YourViewController.m file, implement the following instance method:
放弃应用程序中的任何文本字段
这种方法很干净并且可以保证工作,因为根据定义,keyWindow 是显示键盘的所有可能视图的根视图(来源):
To resign any text field in the app
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):
这将放弃一个特定的文本字段
要放弃任何文本字段,请使用以下代码
This will resign one particular text field
To resign any text field use below code
作为最后的手段
as a last resort ????
如果您不知道哪个文本字段是第一响应者,您可以找到它。我使用这个函数:
然后在你的代码中调用:
If you don't know which textField is the first responder you can find it. I use this function:
Then in your code call:
您只需将
yourTextFieldName
替换为,您猜对了!你的文本字段。这将关闭键盘。You just replace
yourTextFieldName
with, you guessed it! your textfield. This will close the keyboard.使用 didEndOnExit 调用此方法 (methodName)
call this method (methodName) with didEndOnExit
对于Swift 3,
你可以像这样隐藏键盘:
如果你想在用户按下“intro”按钮时隐藏键盘,你必须实现以下
UITextFieldDelegate
方法:For Swift 3
You can hide the keyboard like this:
If you want to hide the keyboard when the user press the "intro" button, you have to implement the following
UITextFieldDelegate
method:3简单&快速步骤
将
UITextFieldDelegate
添加到您的类中,如下所示:在类实现中,添加委托函数
textFieldShouldEndEditing:
:最后一步,设置您的
UITextField 在适当的地方委托给自己。例如,在 viewDidLoad 函数中:
现在,每当用户按下返回键时,键盘就会消失。
我也准备了一个示例片段,您可以从此处查看它< /a>.
3 Simple & Swift steps
Add
UITextFieldDelegate
to your class as below:in class implementation, add the delegate function
textFieldShouldEndEditing:
:and as a last step, set your
UITextField
(s) delegate(s) to self, in somewhere appropriate. For example, inside theviewDidLoad
function:Now, whenever user hits the return key, keyboard will dismiss.
I prepared an example snippet too, you can check it from here.
在 Xcode 中设置“退出时结束”事件(右键单击文本字段)。
实现该方法:
Set up the "Did End On Exit" event in Xcode (right click on your text field).
Realize this method: