如何在不按 Return 键或任何其他可控按钮的情况下关闭键盘
我看过很多关于如何在按回车键或操作某些按钮时隐藏键盘的示例。
但我的情况有所不同:如何仅在文本字段外部单击即可关闭键盘?想象一下用户点击了文本字段来输入一些文本...但他后悔了,只想隐藏键盘并继续查看屏幕,就像点击文本字段之前一样
我不知道该怎么做
提前感谢
佩德罗
I have seen many samples on how to hide the keyboard when pressing return or actioning some button.
But my case is different: How do I dismiss the keyboard just clicking outside the TextField? Imagine the user has tapped a TextField to enter some text... But he regrets and just want to hide the keyboard and continue viewing the screen as before tapping in the textfield
I don't find how to do it
Thanks in advance
Pedro
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
实施touchesBegan并放弃作为第一响应者的文本字段。
如果您有多个文本字段,则应在 activeTextField 实例变量中存储指向当前活动文本字段的指针。为此,您必须将 self 设置为所有文本字段的委托并实现此委托方法:
Implement touchesBegan and resign the text field as first responder.
If you have multiple text fields, you should store a pointer to currently active one in activeTextField instance variable. To do that, you would have to set self as delegate for all text fields and implement this delegate method:
试试这个代码。
Try this code.
您可以在事件外部进行润色时对第一响应者进行签名。
否则看看这个答案
iPhone,触摸 UITextField 外部时关闭键盘
You can rasignfirstresponder on touch up outside event.
Otherwise have a look at this answer
iphone, dismiss keyboard when touching outside of UITextField