有没有办法在同一个 iOS Xcode 项目中使用 storyboard 和 SwiftUI?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/56529488/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 10:58:17  来源:igfitidea点击:

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

xcodestoryboardswiftui

提问by Nirbhay Singh

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

由于 Swift 5 引入了用于创建视图的 SwiftUI 框架,但我们目前正在使用故事板进行 UI 设计。

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

所以我只想知道在同一个 iOS 单视图应用程序中使用 Storyboard 和 Swift UI 的过程。

采纳答案by Mike.R

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

我刚开始看SwiftUI. 分享一个小例子。

  1. In the storyboardadd Hosting View Controller
  2. Subclass the UIHostingControllerwith your own class (ChildHostingController) enter image description here
  3. ChildHostingControllershould look something like that:
  1. storyboard添加Hosting View Controller
  2. 子类中UIHostingController有你自己的类(ChildHostingController在此处输入图片说明
  3. ChildHostingController应该看起来像这样:

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

有关更多详细信息,请查看Custom UIHostingController
Apple Docs UIhostingController(遗憾的是尚未记录)
Integrating SwiftUI Video

回答by SinaMN75

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

正如 Edgell 所提到的,有一个新ViewController名称HostViewController可以在其中托管一个SwiftUI页面。在 WWDC 上有一个关于在现有项目中集成 SwiftUI 的完整讨论,可以很好地回答您的问题。

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

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

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

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

回答by J. Edgell

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.

您需要向情节提要中添加一个“主机视图控制器”。SwiftUI 表单将显示在 Host View Controller 中,并且可以从任何 Storyboard 表单中调用。

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.

请注意,主机视图控制器不会显示在 Mohave 上的 Xcode 11 库中,您必须在 Catalina 上。这是一个错误,因为一旦您在 Catalina 上创建了一个带有 Host View Controller 的项目,同一个项目将在 Mohave 上运行良好,事实上,您甚至可以将该 Host View Controller 复制到其他 Storyboard,它会很好地工作。

回答by Anastasiia Staiko

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. 转到您当前的 Xcode 项目 -> Storyboard,单击 + 号(右上角)并搜索Hosting Controller(就像您搜索按钮或标签一样)。

  2. 将托管控制器拖到您的故事板。创建从您的 UI 元素(我正在使用按钮)到该托管控制器的 Segue 连接,然后选择推送。创建从该 Segue 到您的视图控制器的插座连接(这是一个新功能 - 就像您为标签创建插座一样),并为其命名。

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.
  1. 在此出口连接内声明您的视图(您可以这样做,不必使用 PrepareForSegue 方法),然后返回它。

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:

例如:我在当前项目中创建了一个 SwiftUI 视图(在 Xcode 中:文件 -> 新建 -> 文件 -> SwiftUI 视图)并将其命名为 DetailsView。我的插座连接看起来像这样:

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.

就是这样!现在运行它。

回答by Goga Barabadze

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

您可以在同一个项目中使用两者而不会出现问题。

SwiftUIis just a framework. You can create one ViewControllerwith SwiftUIand another one with the storyboard. The choice is yours.

SwiftUI只是一个框架。您可以ViewController使用SwiftUI创建一个,另一个使用storyboard。这是你的选择。

Just import SwiftUIwhere you want to use it

只需在您想要使用的地方导入SwiftUI