有没有办法在同一个 iOS Xcode 项目中使用 storyboard 和 SwiftUI? [英] Is there any way to use storyboard and SwiftUI in same iOS Xcode project?
问题描述
由于 Swift 5 引入了用于创建视图的 SwiftUI 框架,但我们目前正在使用故事板进行 UI 设计.
所以我只想知道在同一个 iOS 单视图应用程序中使用 Storyboard 和 Swift UI 的过程.
我刚刚开始研究 SwiftUI
.分享一个小例子.
- 在
storyboard
中添加Hosting View Controller
- 使用您自己的类 (
ChildHostingController
) 子类化UIHostingController
ChildHostingController
应该看起来像这样:
<预><代码>导入 UIKit导入 SwiftUI结构SecondView:查看{var主体:一些视图{虚拟堆栈{文本(第二个视图").字体(.系统(大小:36))Text("Loaded by SecondView").font(.system(size: 14))}}}类 ChildHostingController: UIHostingController 有关更多详细信息,请查看自定义 UIHostingController 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
For more details have a look at Custom UIHostingController 这篇关于有没有办法在同一个 iOS Xcode 项目中使用 storyboard 和 SwiftUI?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
Apple 文档 UIhostingController(遗憾的是尚未记录)
集成 SwiftUI 视频SwiftUI
. Sharing a small example.
storyboard
add Hosting View Controller
UIHostingController
with your own class (ChildHostingController
)
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()
}
}
Apple Docs UIhostingController (Unfortunatelly it hasn't been documented yet)
Integrating SwiftUI Video