Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

As Swift 5 introduces the SwiftUI framework for creating the views, but we are currently using the storyboard for UI design.

So I just wanted to know the procedure to use Storyboard and Swift UI in same iOS Single View Application.

like image 566
Nirbhay Singh Avatar asked Jun 10 '19 15:06

Nirbhay Singh


People also ask

Can you use both SwiftUI and storyboard?

No matter what the case is, it's really easy to bring SwiftUI to storyboard based apps and enjoy the benefits it has to offer. This can be done in two ways; either to embed SwiftUI views as NSViews, or as view controllers.

Can we use SwiftUI in existing project?

This SwiftUI UIKit integration solution can provide the experience of using SwiftUI without creating a new app from scratch. Adding SwiftUI to an existing UIKit project will also make it easier for us to migrate the app to SwiftUI in the future.

Does SwiftUI replace storyboard?

There is no direct way of migrating from storyboard to swift UI. This compels a developer to continue the use of the storyboard for existing apps and in lower versions. Since, it is very new so there is less community support which is not the case for storyboard.


2 Answers

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) enter image description here
  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

like image 76
Mike.R Avatar answered Sep 22 '22 12:09

Mike.R


Yes you can do that! Here are the steps you can take to do so:

  1. Go to your current Xcode project -> Storyboard, click on the + sign (right upper corner) and search for Hosting Controller (just like you would for a button or label).

  2. Drag Hosting Controller to your Storyboard. Create a Segue connection from your UI element (I'm using a button) to that Hosting Controller and select Push. Create an outlet connection from that Segue to your View Controller (it's a new feature - just like you would create an outlet for a Label), and name it.

enter image description here

  1. Declare your view inside of this outlet connection (you can do that, don't have to use PrepareForSegue method), and return it.

For example: I created a SwiftUI view in my current project (in Xcode: File -> New -> File -> SwiftUI View) and called it DetailsView. My outlet connection would look like this:

import UIKit  import SwiftUI  class ViewController: UIViewController {      @IBSegueAction func showDetails(_ coder: NSCoder) -> UIViewController? {         let detailsView = DetailsView()         return UIHostingController(coder: coder, rootView: detailsView)     }      override func viewDidLoad() {         super.viewDidLoad()          // some code      } } 

That's it! Now run it.

like image 39
Anastasiia Staiko Avatar answered Sep 20 '22 12:09

Anastasiia Staiko