How to determine children of a UIHostingController embedded within UINavigationController?

174 Views Asked by At

I'm writing an app that's uses SwiftUI for most of its views, however due to a quite "out there" design I had to use UIKit for a custom navbar and tabbar implementations. Several of the controls shown at all times (often overlaid over the app's contents) need to react to the View currently being shown in the main part of the app. So far I've been successful with keeping track of the root views, and reacting to views being pushed and popped.

The basic structure looks as follows: MainViewController embeds two separate navigation controllers at the same time. One hosts the entire "sidebar" flow of the app, the other hosts the main content of the app. Both of these UINavigationControllers point to MainViewController as their delegate to allow for interactive animations using

extension MainViewController: UINavigationControllerDelegate {
    
    func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
        if let coordinator = navigationController.topViewController?.transitionCoordinator {
            coordinator.animate(alongsideTransition: { context in
                // animations here
            }, completion: { context in
                // completion block here
            })
        }
        else if navigationWrapper.viewControllers.count == 1 {
            // being at root stuff here
        }
    }
}

So far it's been working great, the animations work good, react to being cancelled properly too.

Right now however, I need to determine exactly what Views are becoming visible. Casting viewController in the delegate method to UIHostingControler<Content:View> requires me to provide the generic View type. It works, but only for the root views. Whenever I navigate deeper using NavigationLink, the UIHostingController seems to hold an instance of type BridgedPresentation.RootView as its root view. I can't find any documentation on that.

How would I go about determining the exact type of View being pushed using a NavigationLink from a View that's contained in UINavigationController?

0

There are 0 best solutions below