0

I create the custom alert, but when I try to show it on .sheet View, I have this error -

2023-08-05 00:21:15.880778+0200 MyApp[759:72430] [Presentation] Attempt to present <_TtGC7SwiftUI19UIHostingControllerV6Events11CustomAlert_: 0x108009600> on <_TtGC7SwiftUI19UIHostingControllerGVS_15ModifiedContentVS_7AnyViewVS_12RootModifier__: 0x103022800> (from <_TtGC7SwiftUI19UIHostingControllerGVS_15ModifiedContentVS_7AnyViewVS_12RootModifier__: 0x103022800>) which is already presenting <_TtGC7SwiftUI29PresentationHostingControllerVS_7AnyView_: 0x10680a000>.

this is my code -


extension View {
    func customAlert(args) -> some View {
        if let topController = UIApplication.shared.windows.first?.rootViewController {
           let alert = CustomAlert(args)
           let hostingController = UIHostingController(rootView: alert)
           hostingController.modalPresentationStyle = .overFullScreen
           hostingController.modalTransitionStyle = .crossDissolve
           hostingController.view.backgroundColor = .clear

           topController.present(hostingController, animated: false)
        }

        return self
     }
}

how can I fix it?

please help me! I don't now, why is it happing:(

Michael Hulet
  • 3,253
  • 1
  • 16
  • 33
developer
  • 3
  • 2
  • It's likely that the root view controller of that window is already attempting to present another view controller modally (for example, in a sheet or similar, or even another one of these alerts) when this function is called. However, note that it's not safe to just grab the first window, as your app could have multiple windows, and this could cause the alert to be presented on the wrong one – Michael Hulet Aug 04 '23 at 22:38

1 Answers1

1

.sheet modifier presents a view controller modally on the current window's root view controller. When we need to present a view controller on top of already presented view controller we can use presentedViewController property of UIViewController to check if the root view controller has already presented any VC.

extension View {
    func customAlert(args) -> some View {
        if let topController = UIApplication.shared.windows.first?.rootViewController {
            let alert = CustomAlert(args)
            let hostingController = UIHostingController(rootView: alert)
            hostingController.modalPresentationStyle = .overFullScreen
            hostingController.modalTransitionStyle = .crossDissolve
            hostingController.view.backgroundColor = .clear
            
            // check if a VC is already presented modally
            if let presentedVC = topController.presentedViewController {
                presentedVC.present(hostingController, animated: false)
            } else {
                topController.present(hostingController, animated: false)
            }
        }
        
        return self
    }
}

Palwandew
  • 70
  • 1
  • 7