如何从 MODAL 视图控制器隐藏 iPad 键盘?
我试图从模态视图控制器中隐藏 iPad 键盘,但它不起作用。我尝试过 resignFirstResponder 但如果我们处于模态视图控制器中则不会产生任何影响。我尝试在非模态 UINavigationController 中使用 resignFirstResponder 与完全相同的 UIViewController 进行操作,并且键盘正确隐藏。
有谁知道如何解决这个问题?
谢谢。
[更新] 看起来我的代码有问题,因为 resignFirstResponder 确实有效(我做了一个简单的测试用例,而不是使用我的代码)。但我还是不知道问题出在哪里。
I'm trying to hide the iPad keyboard from a modal view controller but it doesn't work. I have tried resignFirstResponder but that doesn't have any affect if we are in a modal view controller. I tried resignFirstResponder in a non-modal UINavigationController with the very same UIViewController and the keyboard hides correctly.
Does anyone know how solve this problem?
Thanks.
[Update] it looks like there's something wrong with my code because the resignFirstResponder does work (I made a simple test case instead of using my code). But I still don't know what the problem is.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
显然,有一个新的
-[UIViewController disablesAutomaticKeyboardDismissal]
方法,您可以重写该方法来解决 iOS 4.3 中的此问题。Apparently, there is a new
-[UIViewController disablesAutomaticKeyboardDismissal]
method that you may override to solve this problem in iOS 4.3.这是因为我正在使用 UIModalPresentationFormSheet。所有其他的都按预期工作......为此浪费了几个小时。
It was because I was using UIModalPresentationFormSheet. All of the other ones work as expected.... Wasted several hours on that.
找到这个真是太痛苦了。看起来像是 iOS 中最糟糕的 API 设计之一。非常感谢@0xced 和@manicaesar 的回答。
这是我对那些陷入困境的未来开发者的综合答案。
如果它是单个视图控制器,只需覆盖
disablesAutomaticKeyboardDismissal
并返回 NO。如果它是模态中的导航控制器,请创建您自己的 UINavigationController 子类,如下所示:
In .h...
In .m...
在显示模态视图控制器的代码中。
This was a total pain to find. Seems like one of the poorer API designs in iOS. Much appreciation to @0xced and @manicaesar for the answers.
Here's my consolidated answer for future devs who are stuck beating their head against the wall.
If it's a single view controller, just override
disablesAutomaticKeyboardDismissal
and return NO.If it's a navigation controller in a modal, create your own UINavigationController subclass like so:
In .h...
In .m....
In your code that shows a modal view controller.
我刚刚确认问题确实是 UIModalPresentationFormSheet 并向 apple rdar://8084017 提交了错误报告
I just confirmed the problem is indeed UIModalPresentationFormSheet and filed a bug report to apple rdar://8084017
我通过调整 UIModalPresentationPageSheet 的大小解决了这个问题。请参阅我的回答此处。
I solved this by resizing a UIModalPresentationPageSheet. See my answer here.