2
votes

In iOS 13,there is a new behaviour for modal view controller when being presented. Now it's not fullscreen by default, and when i try to change modalPresentationStyle to .fullScreen my view present and dismiss immediately. I'm presenting view controller with code :

 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)
}
1
Why are you creating a new window just to show a view controller? That UIWindow initializer isn't valid under iOS 13.rmaddy
How could i present my view controller on top, iOS 12 i using UIWindow and it work fine, but iOS 13 what should i do?Son Pham
Just present the view controller from the current top-most view controller. That will work under any iOS version. No extra window needed.rmaddy

1 Answers

5
votes

I found the solution:

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)
}