Swift 中显示 UIAlertcontroller 时保持键盘打开?
KEEP keyboard ON when UIAlertcontroller is presented in Swift?
当警报弹出时,键盘被关闭。我到处都看过,但没有找到保持键盘可见的解决方案。当出现警报时,文本字段似乎会自动退出第一响应者,因为警报是模态出现的。怎样才能让键盘保持在这个警报后面,这意味着即使无法进行交互,文本字段仍在编辑?
这个解决方案适合我:
let rootViewController: UIViewController =
UIApplication.sharedApplication().windows.lastObject.rootViewController!!
rootViewController.presentViewController(alert, animated: true, completion: nil)
@galambalazs 编辑: 它起作用的原因是:
您可以获取当前最高 window 级别的 window 并在 [=34= 中展示您的 View Controller ](使其成为顶部View Controller中的顶部Window)。
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.
您可能还想设置 window 的 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;
For Swift 3 and 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)
}
当警报弹出时,键盘被关闭。我到处都看过,但没有找到保持键盘可见的解决方案。当出现警报时,文本字段似乎会自动退出第一响应者,因为警报是模态出现的。怎样才能让键盘保持在这个警报后面,这意味着即使无法进行交互,文本字段仍在编辑?
这个解决方案适合我:
let rootViewController: UIViewController =
UIApplication.sharedApplication().windows.lastObject.rootViewController!!
rootViewController.presentViewController(alert, animated: true, completion: nil)
@galambalazs 编辑: 它起作用的原因是:
您可以获取当前最高 window 级别的 window 并在 [=34= 中展示您的 View Controller ](使其成为顶部View Controller中的顶部Window)。
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.
您可能还想设置 window 的 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;
For Swift 3 and 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)
}