How to Add Storyboard Viewcontroller into Swiftui Project

Is there any way to use storyboard and SwiftUI in same iOS Xcode project?

I just started to look at the SwiftUI. Sharing a small example.

  1. In the storyboard add Hosting View Controller
  2. Subclass the UIHostingController with your own class (ChildHostingController)
    Sample Image
  3. ChildHostingController should look something like that:

import UIKit
import SwiftUI

struct SecondView: View {
var body: some View {
VStack {
Text("Second View").font(.system(size: 36))
Text("Loaded by SecondView").font(.system(size: 14))
}
}
}

class ChildHostingController: UIHostingController<SecondView> {

required init?(coder: NSCoder) {
super.init(coder: coder,rootView: SecondView());
}

override func viewDidLoad() {
super.viewDidLoad()
}
}

For more details have a look at Custom UIHostingController

Apple Docs UIhostingController (Unfortunatelly it hasn't been documented yet)

Integrating SwiftUI Video

How to add Storyboard ViewController into SwiftUI Project?

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

}

Integrating UIViewController with Storyboard into SwiftUI Project: A Gap at the Top after Transition

I assume it is empty large header area of NavigationView, so just hide it

    NavigationView {
VStack {
NavigationLink(destination: HomeViewControllerRepresentation()) {
Text("Tap me")
}
}
.navigationBarTitle("")
.navigationBarHidden(true)
}

Is there any way to use storyboard and SwiftUI in same iOS Xcode project for Apple Watch?

WKHostingController is the WatchKit equivalent of UIHostingController.

If you use WKHostingController, you'll be able to use a SwiftUI view in a storyboard on watchOS.



Related Topics



Leave a reply



Submit