在 UITextField 中按下清除按钮时隐藏键盘
当按下 UITextField
的清除按钮时,有什么方法可以隐藏键盘吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
当按下 UITextField
的清除按钮时,有什么方法可以隐藏键盘吗?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(6)
是的,有,尽管我怀疑这样做会违反苹果人机界面指南。
为此,请将以下方法添加到视图控制器的实现文件中。然后将视图控制器放入文本字段的委托中。
这种方法的缺点是,如果您想阻止清除文本字段,您的代码就会变得混乱。相反,您可以尝试定义一个自定义方法,然后将其连接到
valueDidChange
方法并检查是否有空值。这里的问题是,当 UITextField 中有一个字符时,您无法轻松区分点击清除按钮和点击删除按钮。
正如我在答案开头所说,这从一开始就是不可取的,而且正如这里的答案所示,实施起来并不容易。考虑到所涉及的困难以及它不会带来最佳用户体验的事实,我认为不值得这么麻烦。
Yes, there is, although I suspect that doing so would violate the Apple Human Interface Guidelines.
To do so, add the following method to your view controller's implementation file. Then make the view controller into your textfield's delegate.
The downside to this approach is if you ever want to prevent the textfield from clearing, your code becomes messy. Instead you might try to define a custom method and then connect it to the
valueDidChange
method and check for an empty value.The problem here is that you can't easily differentiate between a tap on the clear button and a tap on the delete button when there is one character in the UITextField.
As I said in the beginning of my answer, this is not advisable in the first place and as the answers here have shown, it is not so easy to implement. I don't think it's worth the hassle, considering the difficulty involved and the fact that it doesn't result in optimal user experience.
这段代码绝对适合我隐藏键盘,同时清除文本字段的内容
This code is definitely working for me to hide the key board while clearing out the content of the textfield
是的。调用
textFieldShouldClear:
方法。Yep. Call
resignFirstResponder
on the text field in the delegate'stextFieldShouldClear:
method.在 UITextFieldDelegate 中
但是这样有一个问题。根据手册,“文本字段调用此方法来响应用户按下内置清除按钮。(默认情况下不显示此按钮,但可以通过更改文本字段的clearButtonMode属性中的值来启用。)当编辑开始并且文本字段的clearsOnBeginEditing 属性设置为YES 时,也会调用此方法。”
请注意,如果clearsOnBeginEditing 设置为YES,则在编辑开始时调用此方法。因此,如果您在此方法中调用 resignFirstResponder ,那么编辑实际上不会开始。所以你需要将clearsOnBeginEditing设置为NO。显然,编辑开始时文本字段不会被清除。
另一个与问题没有直接关系的重要事项。点击清除按钮后隐藏键盘并不是一种常见的行为,苹果也不喜欢改变标准项目的行为。您可能会因此被拒绝。
In UITextFieldDelegate
But there is a problem with this. From the manual, "The text field calls this method in response to the user pressing the built-in clear button. (This button is not shown by default but can be enabled by changing the value in the clearButtonMode property of the text field.) This method is also called when editing begins and the clearsOnBeginEditing property of the text field is set to YES."
Note that, this method is called when editing begins if clearsOnBeginEditing is set to YES. So if you call resignFirstResponder in this method then editing will not begin actually. So you need to set clearsOnBeginEditing to NO. Obviously then the text field won't be cleared when editing begins.
Another IMPORTANT matter not directly related to the question. Hiding the keypad after tapping clear button is not a familiar behavior and Apple does NOT like changing the behavior of standard items. You may get a rejection for this.
试试这个代码:
Try this code:
对于 Swift
在你的 UITextFieldDelegate 中
For Swift
In your UITextFieldDelegate