【发布时间】:2015-04-18 08:07:09
【问题描述】:
当警报弹出时,键盘被关闭。我到处寻找,但没有找到保持键盘可见的解决方案。当警报出现时,文本字段似乎会自动退出第一响应者,因为警报以模态方式出现。如何将键盘保持在此警报后面,这意味着即使无法进行交互,文本字段仍在编辑?
【问题讨论】:
标签: ios swift keyboard uialertcontroller presentviewcontroller
当警报弹出时,键盘被关闭。我到处寻找,但没有找到保持键盘可见的解决方案。当警报出现时,文本字段似乎会自动退出第一响应者,因为警报以模态方式出现。如何将键盘保持在此警报后面,这意味着即使无法进行交互,文本字段仍在编辑?
【问题讨论】:
标签: ios swift keyboard uialertcontroller presentviewcontroller
这个解决方案对我有用:
let rootViewController: UIViewController =
UIApplication.sharedApplication().windows.lastObject.rootViewController!!
rootViewController.presentViewController(alert, animated: true, completion: nil)
@galambalazs 编辑: 它起作用的原因是:
您可以抓取当前最高窗口级别的窗口,并将您的 View Controller 显示在该 Window 中(使其成为顶部 View Controller在顶部窗口)。
UIApplication.sharedApplication().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;
【讨论】:
UIAppplication.sharedApplication().windows 中的第二个窗口是 UITextEffectsWindow。
适用于 Swift 3 和 iOS11
if let alertWindow = UIApplication.shared.windows.last, alertWindow.windowLevel == 10000001.0 // If keyboard is open
{ // Make sure keyboard is open
alertWindow.rootViewController?.present(alertController, animated: true, completion: nil)
}
else
{
viewController?.present(alertController, animated: true, completion: nil)
}
【讨论】: