4

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)
            }
        }
    }
}

1 Answer 1

2

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) {
    
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.