“UIHostingView未能呈现已经在呈现的视图”

huangapple go评论96阅读模式
英文:

UIHostingView failed to present view, which already presenting

问题

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 happening:(

英文:

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:(

答案1

得分: 1

.sheet修饰符在当前窗口的根视图控制器上以模态方式呈现视图控制器。当我们需要在已经呈现了视图控制器的基础上呈现另一个视图控制器时,我们可以使用UIViewControllerpresentedViewController属性来检查根视图控制器是否已经呈现了任何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
            
            // 检查是否已经以模态方式呈现了VC
            if let presentedVC = topController.presentedViewController {
                presentedVC.present(hostingController, animated: false)
            } else {
                topController.present(hostingController, animated: false)
            }
        }
        
        return self
    }
}
英文:

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

huangapple
  • 本文由 发表于 2023年8月5日 06:34:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76839428.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定