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

时间:2018-02-07 21:05:38

标签: swift uitabbar

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

我想在没有storyBoard的TableViewController CreateElement 上添加一个TabBar ONLY

元素 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 显示自定义标签栏时,我没有成功。

有任何建议如何进行?

1 个答案:

答案 0 :(得分:1)

元素 TableViewController中调用tableview的didSelect时,按下UITabBarController而不是 CreateElement tableViewController。

来自元素 didSelectRowAt indexPath TableViewController

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

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

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]. 
    }
}