我正在尝试使用 UIViewControllerRepresentable 将 UITabBarController 包装在SwiftUI视图中,但是遇到了我几天都无法解决的问题。我仔细研究了每个可能的线程,并尝试了所有建议的解决方案,但都没有碰到运气。任何帮助将不胜感激。
TabViewController.swift
import UIKit
import ResearchKit
import CareKit
import SwiftUI
struct TabBarView: UIViewControllerRepresentable {
func makeUIViewController(context: Context) -> UITabBarController {
let tabBarViewController = TabBarViewController()
return tabBarViewController
}
func updateUIViewController(_ uiViewController: UITabBarController, context: Context) {
}
typealias UIViewControllerType = UITabBarController
}
class TabBarViewController: UITabBarController {
fileprivate let carePlanStoreManager = CarePlanStoreManager.sharedCarePlanStoreManager
fileprivate let carePlanData: CarePlanData
fileprivate var symptomTrackerViewController: OCKSymptomTrackerViewController? = nil
fileprivate var insightsViewController: OCKInsightsViewController? = nil
fileprivate var insightChart: OCKBarChart? = nil
required init?(coder aDecoder: NSCoder) {
carePlanData = CarePlanData(carePlanStore: carePlanStoreManager.store)
super.init(coder: aDecoder)
carePlanStoreManager.delegate = self
carePlanStoreManager.updateInsights()
let careCardStack = createCareCardStack()
let symptomTrackerStack = createSymptomTrackerStack()
let insightsStack = createInsightsStack()
let connectStack = createConnectStack()
self.viewControllers = [careCardStack,
symptomTrackerStack,
insightsStack,
connectStack]
tabBar.tintColor = UIColor.systemTeal
tabBar.barTintColor = UIColor.white
}
答案 0 :(得分:1)
尝试按以下步骤将所有内容移至init
中(在这种情况下,您不需要编码器)
class TabBarViewController: UITabBarController {
fileprivate let carePlanData: CarePlanData
fileprivate var symptomTrackerViewController: OCKSymptomTrackerViewController? = nil
fileprivate var insightsViewController: OCKInsightsViewController? = nil
fileprivate var insightChart: OCKBarChart? = nil
fileprivate let carePlanStoreManager = CarePlanStoreManager.sharedCarePlanStoreManager
init() {
carePlanData = CarePlanData(carePlanStore: carePlanStoreManager.store)
super.init(nibName: nil, bundle: nil)
carePlanStoreManager.delegate = self
carePlanStoreManager.updateInsights()
let careCardStack = createCareCardStack()
let symptomTrackerStack = createSymptomTrackerStack()
let insightsStack = createInsightsStack()
let connectStack = createConnectStack()
self.viewControllers = [careCardStack,
symptomTrackerStack,
insightsStack,
connectStack]
tabBar.tintColor = UIColor.systemTeal
tabBar.barTintColor = UIColor.white
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}