Present embedded SwiftUI Subview in Full Screen, UIHostingController

866 views Asked by At

I work on a Storyboard Swift App want to present a SwiftUI Subview in Full-Screen. I embedded the SwiftUI View using UIHostingController, like this:

let contentView = UIHostingController(rootView: ContentView())

override func viewDidLoad() {
    super.viewDidLoad()
    addChild(contentView)
    view.addSubview(contentView.view)
 }

And this is my SwiftUI ContentView() Subview:

import SwiftUI
import UIKit

struct ContentView: View {
    var body: some View {
        Text("").fullScreenCover(isPresented: /*@START_MENU_TOKEN@*/.constant(true)/*@END_MENU_TOKEN@*/, content: {
            FullScreenView.init()
        })
    }
}

struct FullScreenView: View{
    var body: some View {
        NavigationView{
            MasterView()
        }.navigationViewStyle(DoubleColumnNavigationViewStyle())
    }
    }

struct MasterView: View {
     var body: some View {
            Form {
                Section(header: Text("HEADER")) {
                    Section {
                        NavigationLink(destination: UIKitView()) { Text("NAVLINK TEXT") }

                    }
                
                }
            }
            .navigationBarTitle("NAVBAR TEXT")
    }
}

struct UIKitView: UIViewControllerRepresentable {
    typealias UIViewControllerType = SwipeViewController

    func makeUIViewController(context: Context) -> SwipeViewController {
        let sb = UIStoryboard(name: "Storyboard", bundle: nil)
        let viewController = sb.instantiateViewController(identifier: "vc") as! SwipeViewController
        return viewController
    }
    
    func updateUIViewController(_ uiViewController: SwipeViewController, context: Context) {
        
    }
}

Is there a better way to do it? As you can see, I use an empty text field to add the .fullScreenCover :/

Thanks for any help!!

0

There are 0 answers