[英]How do I add a SwiftUI view to an existing Tab Bar Controller
I have an app that uses storyboard, I need one of the tabs to be SwiftUI, how can I add it to my existing project?我有一个使用 storyboard 的应用程序,我需要其中一个选项卡为 SwiftUI,如何将其添加到我现有的项目中?
I assume someone will need to know this at some point,我想有人会在某个时候需要知道这一点,
step 1. Add a Hosting View Controller to your storyboard步骤 1. 将托管视图 Controller 添加到您的 storyboard
step 2. Create a root view controller relationship segue between your navigation controller/tab bar controller and the HostingView Controller步骤 2. 在导航控制器/标签栏 controller 和 HostingView Controller 之间创建根视图 controller 关系segue
step 3. create your SwiftUI Class步骤 3. 创建您的 SwiftUI Class
import SwiftUI
struct AnalyticsView: View {
var body: some View {
Text("Hello")
}
}
struct AnalyticsView_Previews: PreviewProvider {
static var previews: some View {
("Hello World")
}
}
step 4. create a UIHostingController class and set the HostingViewController to that class in the class inspector步骤 4. 创建 UIHostingController class 并在 class 检查器中将 HostingViewController 设置为 class
import UIKit
import SwiftUI
class AnalyticsVC: UIHostingController<AnalyticsView> {
required init?(coder aDecoder: NSCoder){
super.init(coder: aDecoder, rootView: AnalyticsView())
}
}
step 5. build your view controller from your SwiftUI view步骤 5. 从 SwiftUI 视图构建视图 controller
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.