Present a floating panel modally in SwiftUI
masterTo present a floating panel modally in SwiftUI, you must implement a custom coordinator by conforming to the FloatingPanelCoordinator protocol.
Inside setupFloatingPanel, you configure the contentHostingController and use a Task to present the controller on the next run loop cycle to ensure the view hierarchy is correctly established.
struct HomeView: View {
var view: some View {
MainView()
.floatingPanel(
coordinator: MyPanelCoordinator.self
) { proxy in
...
}
}
}
class MyPanelCoordinator: FloatingPanelCoordinator {
...
func setupFloatingPanel<Main, Content>(
mainHostingController: UIHostingController<Main>,
contentHostingController: UIHostingController<Content>
) where Main: View, Content: View {
// Set the delegate object
controller.delegate = delegate
// Set up the content
contentHostingController.view.backgroundColor = .clear
controller.set(contentViewController: contentHostingController)
/* =============== HERE ==================== */
// NOTE:
// Present the floating panel on the next run loop cycle
// to ensure proper view hierarchy setup.
Task { @MainActor in
mainHostingController.present(controller, animated: false)
}
}
...
}