Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

swift - Present ViewController on top of everything regardless of the view hierarchy in IOS 13

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)
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...