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

后端 未结 5 1238
有刺的猬
有刺的猬 2020-12-13 06:58

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 t

相关标签:
5条回答
  • 2020-12-13 07:12

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

    0 讨论(0)
  • 2020-12-13 07:16

    You need to add a "Host View Controller" to the Storyboard. The SwiftUI form will be displayed in the Host View Controller and is callable from any Storyboard Form.

    Be advised, the Host View Controller does not display in the Library for Xcode 11 on Mohave, you must be on Catalina. This is a bug, because once you have a project with a Host View Controller created on Catalina, that same project will run fine on Mohave, in fact, you can even copy that Host View Controller to other Storyboards and it will wok fine.

    0 讨论(0)
  • 2020-12-13 07:17

    You can use both in the same project without a problem.

    SwiftUI is just a framework. You can create one ViewController with SwiftUI and another one with the storyboard. The choice is yours.

    Just import SwiftUI where you want to use it

    0 讨论(0)
  • 2020-12-13 07:19

    as Edgell Mentioned, there is a new ViewController named HostViewController that can host a SwiftUI page inside it. there's a complete talk about integrating SwiftUI in existing project at WWDC that answers your question very well.

    Integrating SwiftUI: https://developer.apple.com/videos/play/wwdc2019/231/

    WWDC19: https://developer.apple.com/videos/

    0 讨论(0)
  • 2020-12-13 07:20

    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.

    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.

    0 讨论(0)
提交回复
热议问题