I am using UINavigationController
under the hood for my SwiftUI application since NavigationView
and NavigationLink
are extremely limited and/or buggy. I have a function called pushView(..)
that needs to take in a SwiftUI view, wrap it in UIHostingController
and then push it on top of the navigation stack:
func pushView(view: AnyView, animated: Bool = true) {
let hostingView = UIHostingController(rootView: view)
navigationController.pushViewController(hostingView, animated: animated)
}
Now, most of my calls look like ...pushView(AnyView(MyView())
, I have to cast every single view to AnyView
which is a bit annoying and unclean.
Is there a better way to do this so I don't have to cast every single time?