ios - 在 Swift 中显示 UIAlertcontroller 时保持键盘打开?

标签 ios swift keyboard uialertcontroller presentviewcontroller

当警报弹出时,键盘被关闭。我到处都看过,但没有找到保持键盘可见的解决方案。当出现警报时,文本字段似乎会自动退出第一响应者,因为警报是模态出现的。怎么可能将键盘保持在这个警报后面,这意味着即使无法进行交互,文本字段仍在编辑中?

最佳答案

这个解决方案适合我:

let rootViewController: UIViewController = 
    UIApplication.sharedApplication().windows.lastObject.rootViewController!!
rootViewController.presentViewController(alert, animated: true, completion: nil)

由@galambalazs 编辑: 它起作用的原因是:

您可以捕获当前最高窗口级别的窗口,并在该窗口中显示您的View Controller(使其成为顶级View Controller在顶部窗口)。

UIApplication.sharedApplication().windows
The windows in the array are ordered from back to front by window level;
thus, the last window in the array is on top of all other app windows.

您可能还想设置该窗口的 tintColor,使其与您的应用程序的全局 tintColor 相匹配。

UIWindow *topWindow = [UIApplication sharedApplication].windows.lastObject;
// we inherit the main window's tintColor because topWindow may not have the same
topWindow.tintColor = [UIApplication sharedApplication].delegate.window.tintColor;

关于ios - 在 Swift 中显示 UIAlertcontroller 时保持键盘打开?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28564710/

相关文章:

ios - 在 Core Data 中,如何在 swift 4 中执行 `if exists update else insert`?

ios - 快速录音和桌面 View 显示

iOS 胖二进制文件 : should every arch include bitcode?

objective-c - 获取上一个最前面的应用程序

java - 如何以编程方式打开/关闭 Caps Lock、Scroll Lock 和 Num Lock

keyboard - 如何阻止鹿角屏幕键盘打开?

ios - 使 View 可拖动但不可点击

swift - UICollectionView 未从数组中填充数据

arrays - 在闭包中附加到数组

iphone - 在 iPhone 键盘中添加符号