ios UIHostingView无法呈现已呈现的视图

pepwfjgg  于 2023-08-08  发布在  iOS
关注(0)|答案(1)|浏览(107)

我创建了自定义警报,但当我尝试在.sheet View上显示它时,出现了此错误-

2023-08-05 00:21:15.880778+0200 MyApp[759:72430] [演示]尝试<TtGC7SwiftUI19UIHostingControllerV6Events11CustomAlert: 0x108009600>在<TtGC7SwiftUI19UIHostingControllerGVS_15ModifiedContentVS_7AnyViewVS_12RootModifier_: 0x103022800>(来自<TtGC7SwiftUI19UIHostingControllerGVS_15ModifiedContentVS_7AnyViewVS_12RootModifier_: 0x103022800>)已在演示的内容上演示<TtGC7SwiftUI29PresentationHostingControllerVS_7AnyView: 0x10680a000>。

这是我的代码

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

字符串
我该怎么修呢?
请帮帮我!我现在不知道,为什么会发生:)

n3h0vuf2

n3h0vuf21#

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

字符串

相关问题