当UIAlertcontroller在Swift中出现时,KEEP键盘是否打开?

Sam*_*Sam 14 keyboard ios presentviewcontroller swift uialertcontroller

弹出警报时,键盘将被解除.我到处寻找,但没有找到保持键盘可见的解决方案.当提示警报时,文本字段似乎会自动重新响应第一响应者,因为警报是以模态方式呈现的.如何将键盘保持在此警报之后,即使没有可能的交互,文本字段仍然可以编辑?

Sam*_*Sam 27

这个解决方案对我有用:

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

由@galambalazs编辑:它的工作原因是因为:

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

UIApplication.sharedApplication().windows
数组中的窗口按窗口级别从后向前排序;
因此,数组中的最后一个窗口位于所有其他应用程序窗口之上.

您也可以设置该窗口的tintColor,使其与您的应用程序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;
Run Code Online (Sandbox Code Playgroud)

  • 我认为你应该使用UIApplication.sharedApplication().windows.lastObject.rootViewController,然后代码将工作,无论键盘显示或隐藏. (2认同)