ios - 如果模态 ViewController 演示样式为 UIModalPresentationFormSheet,iPad 键盘将不会关闭

标签 ios iphone objective-c uitextfield first-responder

注意:

从 iOS 4.3 开始,请参阅已接受的答案(不是投票最多的答案)。

这个问题是关于在 iPad 键盘中发现的一种行为,如果在带有导航 Controller 的模式对话框中显示它,它会拒绝被关闭。

基本上,如果我向导航 Controller 显示以下行:

navigationController.modalPresentationStyle = UIModalPresentationFormSheet;

键盘拒绝关闭。如果我注释掉这一行,键盘就会消失。

...

我有两个文本字段,用户名和密码;用户名有一个下一步按钮,密码有一个完成按钮。如果我在模态导航 Controller 中显示它,键盘将不会消失。

有效

broken *b = [[broken alloc] initWithNibName:@"broken" bundle:nil];
[self.view addSubview:b.view];

不工作

broken *b = [[broken alloc] initWithNibName:@"broken" bundle:nil];
UINavigationController *navigationController = 
[[UINavigationController alloc]
 initWithRootViewController:b];
navigationController.modalPresentationStyle = UIModalPresentationFormSheet;
navigationController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:navigationController animated:YES];
[navigationController release];
[b release];

如果我删除导航 Controller 部分并将“b”单独呈现为模态视图 Controller ,它就可以工作。是导航 Controller 的问题吗?

有效

broken *b = [[broken alloc] initWithNibName:@"broken" bundle:nil];
b.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:b animated:YES];
[b release];

有效

broken *b = [[broken alloc] initWithNibName:@"broken" bundle:nil];
UINavigationController *navigationController = 
    [[UINavigationController alloc]
         initWithRootViewController:b];
[self presentModalViewController:navigationController animated:YES];
[navigationController release];
[b release];

最佳答案

这已被 Apple 工程师归类为“按预期工作”。不久前我为此提交了一个错误。他们的理由是,用户通常会以模态形式输入数据,因此他们试图“提供帮助”并保持键盘可见,而模态视图中的各种转换通常会导致键盘重复显示/隐藏。

编辑:here is the response开发者论坛上的一位 Apple 工程师:

Was your view by any chance presented with the UIModalPresentationFormSheet style? To avoid frequent in-and-out animations, the keyboard will sometimes remain on-screen even when there is no first responder. This is not a bug.

这给很多人(包括我自己)带来了问题,但目前似乎没有办法解决它。

更新:

在 iOS 4.3 及更高版本中,您现在可以在 View Controller 上实现 `-disablesAutomaticKeyboardDismissal' 以返回 NO:

- (BOOL)disablesAutomaticKeyboardDismissal {
    return NO;
}

这解决了问题。

关于ios - 如果模态 ViewController 演示样式为 UIModalPresentationFormSheet,iPad 键盘将不会关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3372333/

相关文章:

ios - 如何在 UIDocumentInteractionController 上设置 "Copy To (My app)"

objective-c - 解析查询包含非精确字符串

ios - 如何在 Swift 4 中删除 CoreData 的对象

ios - 在 NSIndexPath 上意外发现 nil 运行时错误

ios - 将拍摄的图像分配给 image view.image

Xcode 4.5 的 iOS 模拟器不复制资源

ios - 当我的应用程序在 iPhone 6/6+ 上以原始分辨率运行时,iTunes 将其显示为针对 iPhone 5 优化的应用程序

objective-c - 将按钮图像设置为 Finder 图标

ios - 检查 iOS 上是否安装了 mobileconfig 配置文件

iphone - UITextView 计算不起作用