有没有办法在同一个 iOS Xcode 项目中使用 storyboard 和 SwiftUI? [英] Is there any way to use storyboard and SwiftUI in same iOS Xcode project?

查看:44
本文介绍了有没有办法在同一个 iOS Xcode 项目中使用 storyboard 和 SwiftUI?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

解决方案

我刚刚开始研究 SwiftUI.分享一个小例子.

  1. storyboard中添加Hosting View Controller
  2. 使用您自己的类 (ChildHostingController) 子类化 UIHostingController
  3. ChildHostingController 应该看起来像这样:

<预><代码>导入 UIKit导入 SwiftUI结构SecondView:查看{var主体:一些视图{虚拟堆栈{文本(第二个视图").字体(.系统(大小:36))Text("Loaded by SecondView").font(.system(size: 14))}}}类 ChildHostingController: UIHostingController{需要初始化?(编码器:NSCoder){super.init(coder: coder,rootView: SecondView());}覆盖 func viewDidLoad() {super.viewDidLoad()}}

有关更多详细信息,请查看自定义 UIHostingController
Apple 文档 UIhostingController(遗憾的是尚未记录)
集成 SwiftUI 视频

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.

解决方案

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

这篇关于有没有办法在同一个 iOS Xcode 项目中使用 storyboard 和 SwiftUI?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆