KEEP keyboard ON when UIAlertcontroller is presented in Swift?

£可爱£侵袭症+ 提交于 2019-11-30 04:59:18
Sam

This solution works for me:

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

edit by @galambalazs: The reason it works is because:

You can grab the window with the current highest window level and present your View Controller inside that Window (making it the top View Controller in the top 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.

Also you might want to set the tintColor of that window so that it matches your apps global 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)
  }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!