在iOS 13中,无论视图层次结构如何,都可以将ViewController呈现在所有内容的顶部

7
在iOS 13中,当模态视图控制器被呈现时,有一个新的行为。现在默认情况下它不是全屏的,当我尝试将modalPresentationStyle更改为.fullScreen时,我的视图立即呈现和消失。 我用以下代码呈现视图控制器:
 if #available(iOS 13.0, *) {

        var popupWindow: UIWindow?

        let windowScene = UIApplication.shared
            .connectedScenes
            .filter { $0.activationState == .foregroundActive }
            .first
        if let windowScene = windowScene as? UIWindowScene {
            popupWindow = UIWindow(windowScene: windowScene)
        }

        let vc = UIViewController()
        vc.view.frame = UIScreen.main.bounds

        popupWindow?.frame = UIScreen.main.bounds
        popupWindow?.backgroundColor = .clear
        popupWindow?.windowLevel = UIWindow.Level.statusBar + 1
        popupWindow?.rootViewController = vc
        popupWindow?.makeKeyAndVisible()
        popupWindow?.rootViewController?.present(self, animated: true, completion: nil)
}

你为什么要创建一个新窗口来展示视图控制器?那个UIWindow初始化器在iOS 13下是无效的。 - rmaddy
1
我该如何在iOS 13中将我的视图控制器呈现在最上层?在iOS 12中,我使用UIWindow并且它运行良好,但是在iOS 13中应该怎么办? - Son Pham
2
只需从当前顶部视图控制器中呈现视图控制器即可。这将在任何iOS版本下都起作用。不需要额外的窗口。 - rmaddy
1个回答

13

我找到了解决方案:

if #available(iOS 13.0, *) {
     if var topController = UIApplication.shared.keyWindow?.rootViewController  {
           while let presentedViewController = topController.presentedViewController {
                 topController = presentedViewController
                }
     self.modalPresentationStyle = .fullScreen
     topController.present(self, animated: true, completion: nil)
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接