英文:
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
修饰符在当前窗口的根视图控制器上以模态方式呈现视图控制器。当我们需要在已经呈现了视图控制器的基础上呈现另一个视图控制器时,我们可以使用UIViewController
的presentedViewController
属性来检查根视图控制器是否已经呈现了任何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
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论