Iphone Textview 不调用 TouchesBegan
我有一个文本字段,当我触摸屏幕上的其他位置时(通过我的 TouchesBegan 函数并辞职......等等),它会隐藏键盘。但是当我触摸 Textview 时,TouchesBegan 不会被调用,键盘也不会隐藏!有没有办法调用 TouchesBegan 来隐藏键盘?
I have a Textfield which hides the keyboard when I touch somewhere else on the screen (through my TouchesBegan function and resign...blah). But when I touch a Textview the TouchesBegan does not get invoked and the keyboard doesn't hide! Is there any way to invoke TouchesBegan in order to hide the keyboard?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我假设您的 UITextView 不可编辑,因为您不希望触摸键盘时弹出键盘,因此请确保在 UITextView 的视图属性中未选中“用户交互启用”。您还可以在 ViewController 中这样编写:
这将允许用户事件传递到超级视图,并且将调用 TouchesBegan 和其他委托方法。
I'm assuming your UITextView is not editable as you don't want the keyboard to pop up when you touch it, so then make sure in the View properties for your UITextView that "User Interaction Enabled" is not checked. You can also write this in your ViewController as such:
This will allow user events to pass through to the superview, and touchesBegan and those other delegate methods will be invoked.
还有一种简单的方法可以在touchesBegan上隐藏键盘:当您使用UITextView输入数据时。
步骤 1. 确保在类声明时扩展了 Textviewdelegate。
2. 在视图 did load 函数中将委托设置为 self。
步骤3.在touchesbegan函数中使用你的textView名称编写类似的代码。
谢谢和问候。
There is also a easy way to hide the keyboard on touchesBegan: when you are using UITextView to enter the data.
Step 1. Be sure that you have extended Textviewdelegate while class declaration.
step 2. set the delegate to self in view did load function.
step 3. write the similer code in touchesbegan function with you textView name.
Thanks and regards.
我见过的最好的方法是添加一个透明的子视图,它覆盖文本视图并首先处理 TouchesBegan。然后,您可以检测文本字段外部的触摸,并通过让文本字段辞去第一响应者的身份来关闭键盘。
例如,无论哪种方式,都可以在 IB 中或以编程方式创建覆盖子视图。对其进行放置和调整大小,使其覆盖文本视图,并为其指定清晰的颜色。如果您通过 IB 添加视图,请在主视图加载时隐藏它,以便它还不会吸收触摸,如下所示:
然后,当文本字段开始编辑时,取消隐藏视图:
当文本字段结束编辑时,重新隐藏视图view:
添加一个touchesBegan来检测你的未隐藏的overView何时被触摸:
你也可以通过自定义手势来做到这一点,但这仅适用于iOS 4.x,并且在我看来,更复杂。
The best method I've seen for this is to add a transparent subview that overlays the text view and handles the TouchesBegan first. You can then detect touches outside the text field and dismiss the keyboard by having the text field resign as first responder.
For example, create the overlay subview in IB or programmatically, either way. Place and size it so that it covers the text view(s), and give it a clear color. If you add the view via IB, hide it when your main view loads so that it doesn't absorb touches just yet, like so:
Then when the text field begins editing, unhide the view:
When the text field ends editing, rehide the view:
Add a touchesBegan to detect when your unhidden overView is touched:
You can also do this through custom gestures, but that only works with iOS 4.x and, in my opinion, is more complicated.