如何从 MODAL 视图控制器隐藏 iPad 键盘?

发布于 2024-08-30 21:10:46 字数 309 浏览 6 评论 0原文

我试图从模态视图控制器中隐藏 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

故人的歌 2024-09-06 21:10:46

显然,有一个新的 -[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.

念﹏祤嫣 2024-09-06 21:10:46

这是因为我正在使用 UIModalPresentationFormSheet。所有其他的都按预期工作......为此浪费了几个小时。

It was because I was using UIModalPresentationFormSheet. All of the other ones work as expected.... Wasted several hours on that.

祁梦 2024-09-06 21:10:46

找到这个真是太痛苦了。看起来像是 iOS 中最糟糕的 API 设计之一。非常感谢@0xced 和@manicaesar 的回答。

这是我对那些陷入困境的未来开发者的综合答案。

如果它是单个视图控制器,只需覆盖 disablesAutomaticKeyboardDismissal 并返回 NO。

如果它是模态中的导航控制器,请创建您自己的 UINavigationController 子类,如下所示:

In .h...

@interface MyNavigationController : UINavigationController

@end

In .m...

@implementation MyNavigationController


#pragma mark -
#pragma mark UIViewController
- (BOOL)disablesAutomaticKeyboardDismissal {
    return NO;
}

@end

在显示模态视图控制器的代码中。

UIViewController *someViewController = [[UIViewController alloc] init];

MyNavigationController *navController = [[MyNavigationController alloc] initWithRootViewController:someViewController];

navController.modalPresentationStyle = UIModalPresentationFormSheet;
navController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:navController animated:YES];

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...

@interface MyNavigationController : UINavigationController

@end

In .m....

@implementation MyNavigationController


#pragma mark -
#pragma mark UIViewController
- (BOOL)disablesAutomaticKeyboardDismissal {
    return NO;
}

@end

In your code that shows a modal view controller.

UIViewController *someViewController = [[UIViewController alloc] init];

MyNavigationController *navController = [[MyNavigationController alloc] initWithRootViewController:someViewController];

navController.modalPresentationStyle = UIModalPresentationFormSheet;
navController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:navController animated:YES];

野味少女 2024-09-06 21:10:46

我刚刚确认问题确实是 UIModalPresentationFormSheet 并向 apple rdar://8084017 提交了错误报告

I just confirmed the problem is indeed UIModalPresentationFormSheet and filed a bug report to apple rdar://8084017

陌伤ぢ 2024-09-06 21:10:46

我通过调整 UIModalPresentationPageSheet 的大小解决了这个问题。请参阅我的回答此处

I solved this by resizing a UIModalPresentationPageSheet. See my answer here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文