在iOS13上向场景添加新的UIWindow

Kam*_*iel 5 xcode ios swift ios13

当应用程序进入前台时,我将添加新的UIWindow以显示密码视图控制器。

在iOS 13之前的AppDelegate中,我拥有属性var passcodeWindow = UIWindow(frame: UIScreen.main.bounds),其中我rootViewController是密码视图控制器,并且在applicationWillEnterForeground方法中将passcodeWindow.makeKeyAndVisible()其放置在顶部。

现在,当我想在iOS 13中实现密码功能时,这种方法就会出现问题。我将其移至sceneWillEnterForegroundSceneDelegate中的方法,但似乎无法passcodeWindow在此场景的实际窗口顶部显示。

我执行的操作与AppDelegate中的操作完全相同,并且passcodeWindow未显示。

sceneWillEnterForeground在AppDelegate和SceneDelegate中执行的方式:

passcodeWindow.rootViewController = passcodeViewController(type: .unlock)
passcodeWindow.makeKeyAndVisible()
Run Code Online (Sandbox Code Playgroud)

我希望passcodeWindow显示在场景中当前窗口的顶部。

小智 7

你可以试试这个:

if #available(iOS 13.0, *) {
    if let currentWindowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene {
        passcodeWindow.windowScene = currentWindowScene
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 不起作用,我认为这是因为没有对本地窗口实例的强引用,因此窗口会显示一秒钟,并在下一个运行循环中由于 ARC 为 0 而被释放。 (2认同)