简体   繁体   English

如何将 SwiftUI 视图添加到现有标签栏 Controller

[英]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.

相关问题 如何在现有的 Storyboard 选项卡栏 Z9BBF373797BF7CF7252C8002368 中为 SwiftUI 使用托管 Controller? - How to use Hosting Controller for SwiftUI in existing Storyboard Tab Bar Controller? Swift-如何将Tab Bar和导航栏添加到单个视图控制器? - Swift-How do I add Tab Bar AND Navigation Bar to a single view controller? 如何使用SwiftUI向标签栏项添加图像和标签 - How do I add an image and a label to a tab bar item with SwiftUI 如何更改一个视图控制器的选项卡栏的颜色? - How do I change the color of the tab bar for one view controller? 如何从模态显示的视图 controller 返回到选定的选项卡栏视图 controller? - How do I go back to the selected tab bar view controller from a modally presented view controller? 我在 SwiftUI 中创建了一个自定义选项卡栏,但是当我使用 select 选项卡时,它会完全重新加载视图。 我该如何防止这种情况 - I created a custom tab bar in SwiftUI, but when I select a tab, it completely reloads the view. How do I prevent this 如何显示从 Firebase 检索到的数据到选项卡栏 controller 内的视图 controller 上? - How do I show the data retrieved from Firebase onto a view controller that is inside a tab bar controller? 将标签栏视图连接到视图控制器时,如何创建后退按钮 - How do I create a back button when connecting a tab bar view to a view controller 添加标签栏控制器时,如何防止子视图被切除? - How can I prevent the child view being cut off when I add a tab bar controller? 如何禁用 SwiftUI 中的显示选项卡栏菜单选项 - How do I disable the Show Tab Bar menu option in SwiftUI
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM