uinavigationcontrolleruikitswiftui

SwiftUI : how to access UINavigationController from NavigationView


I am developing an app using SwiftUI. The app is based around a NavigationView.

I am using a third-party framework that provides UIKit components and the framework has not been updated to support SwiftUI yet.

One framework method is expecting a parameter of type UINavigationController

How can I supply this framework the NavigationController created by SwiftUI ? Or how can I create a UINavigationController that will replace SwiftUI's default ?

I read https://developer.apple.com/tutorials/swiftui/interfacing-with-uikit and https://sarunw.com/posts/uikit-in-swiftui but these seems to address another question : they explain how to use UIKit components in a SwiftUI app. My problem is the other way around, I want to use SwiftUI App and access underlying NavigationController object.

[UPDATE] The code implementing my solution is available from this workshop : https://amplify-ios-workshop.go-aws.com/30_add_authentication/20_client_code.html#loginviewcontroller-swift


Solution

  • I don't think you can do that right now. Looking at the view debugger for NavigationView I get the image below.

    So it seems to you will have to go the other way around:
    Start with a UINavigationController, and wrap the SwiftUI view(s) in UIHostingController.

    enter image description here