以编程方式创建 TabBar 但不在 rootViewController 上

Create a TabBar programmatically but not on the rootViewController

我有一个 TableViewController Element,当我点击任何一行时它会推送 tableViewController CreateElement

我想在 TableViewController CreateElement 上添加一个 TabBar ONLY 而没有故事板。

Element TableViewController 上,我添加 didSelectRowAt:

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    let editCreateElementsVC = CreateElementsVC()
    editCreateElementsVC.delegate = self

    let navController = UINavigationController(rootViewController: editCreateElementsVC)
    present(navController, animated: true, completion: nil)
}

我已经创建了一个 CustomizeBarTabController 如下:

    override func viewDidLoad() {
    super.viewDidLoad()

    let mainCollectingFolder = MainCollectingFolders()
    let navigationControllerMainCollectingFolder = UINavigationController(rootViewController: mainCollectingFolder)

    navigationControllerMainCollectingFolder.title = "Folder"
    navigationControllerMainCollectingFolder.tabBarItem.image = UIImage(named: "iconMenu")

    viewControllers = [navigationControllerMainCollectingFolder]
}

我只在 CreateElement.

处显示自定义选项卡栏没有成功

任何建议如何进行?

当在 Element TableViewController 中调用 tableview 的 didSelect 时,推送到 UITabBarController 而不是 CreateElement tableViewController。

来自 didSelectRowAt indexPathElement TableViewController

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let tab = TabbarController()
    self.navigationController?.pushViewController(tab, animated: true)
}

创建 TabbarController,它是 UITabBarController 的子 class,如下所示:

class TabbarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let createElement = CreateElementTVC()      //Create Element TableViewController
        createElement.tabBarItem = UITabBarItem(tabBarSystemItem: .history, tag: 0)

        let more = MoreVC()                         //For eg. take empty UIViewController for another tab
        more.tabBarItem = UITabBarItem(tabBarSystemItem: .more, tag: 1)

        //Assign view controllers which you want to show in tab bar
        self.viewControllers = [createElement, more]. 
    }
}