How to add Storyboard ViewController into SwiftUI Project?

Viewed 2549

I am working on my SwiftUI Project and every View is now in SwiftUI, however due to some limitations of SwiftUI I have to add Storyboard's ViewControllers into my SwiftUI project. I am trying this method,

struct AssetsListView: UIViewControllerRepresentable {

var taskID : String
public typealias UIViewControllerType = AssetsListViewController

func makeUIViewController(context: UIViewControllerRepresentableContext<AssetsListView>) -> AssetsListViewController {
    let assetsListVC = AssetsListViewController()
    assetsListVC.taskID = taskID
    return assetsListVC

}
func updateUIViewController(_ uiViewController: AssetsListViewController, context: UIViewControllerRepresentableContext<AssetsListView>) {


}  
}

This works fine and even viewDidLoad() method of my Storyboard's ViewController calls, but I am unable to see any element on my Storyboard Screen. How can I render those elements? Just like the normal Storyboard stuff.

1 Answers

You just created controller by class initialiser, to instantiate it from storyboard you have to do like the following

func makeUIViewController(context: 
         UIViewControllerRepresentableContext<AssetsListView>) -> AssetsListViewController {
    let storyboard = UIStoryboard(name: "Main",     // < your storyboard name here
          bundle: nil)
    let assetsListVC = storyboard.instantiateViewController(identifier: 
          "AssetsListViewController")      // < your controller storyboard id here

    assetsListVC.taskID = taskID
    return assetsListVC

}
Related