Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Present UINavigationController from SwiftUI

I have a UIViewController that I want presented from my SwiftUI view, the issue is that I need the UIViewController wrapped in a UINavigationController, how can I present that wrapped VC from my SwiftUI View?

This is what I've tried, it works for presenting the view controller but I do not know how to wrap it in a navigation controller:

struct ComposeTakeView: UIViewControllerRepresentable {
    func makeUIViewController(context: Context) -> ComposeTakeVC {
        return ComposeTakeVC(nibName: "ComposeTakeVC", bundle: nil)
    }
    
    func updateUIViewController(_ uiViewController: ComposeTakeVC, context: Context) {
        
    }
}

In the SwiftUI view:

.fullScreenCover(isPresented: $showComposeTake, content: {
                    ComposeTakeView()
                })

I've also tried creating a new SwiftUI view with NavigationView and tool bar, but this does not show the tool bar button:

struct ComposeTakeNavView: View {
    @Environment(\.presentationMode) var presentationMode
    var body: some View {
        NavigationView {
            ComposeTakeView()
        }
        .toolbar {
            
            Button {
                presentationMode.wrappedValue.dismiss()
            } label: {
                Image(systemName: "xmark")
                    .foregroundColor(.black)
            }
        }
    }
}
like image 363
tHatpart Avatar asked Oct 26 '25 05:10

tHatpart


1 Answers

We can wrap it right inside representable and return just as base class, like

func makeUIViewController(context: Context) -> UIViewController {
    let controller = ComposeTakeVC(nibName: "ComposeTakeVC", bundle: nil)
    return UINavigationController(rootViewController: controller)
}

func updateUIViewController(_ uiViewController: UIViewController, context: Context) {
    
}
like image 180
Asperi Avatar answered Oct 29 '25 09:10

Asperi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!