UINavigationController 和 TabBarController 以编程方式(无故事板)

UINavigationController, and TabBarController programmatically (no storyboards)

目前,我的 AppDelegate 文件包含此代码以将 CustomTabBarController 设置为 rootViewController:

window = UIWindow(frame: UIScreen.main.bounds)
window?.makeKeyAndVisible()
window?.rootViewController = CustomTabBarController()

我希望我的应用始终在底部有 CustomTabBarController,但我希望每个选项卡都有一个导航控制器。这是我用来设置 tabBarController 的代码:

class CustomTabBarController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
let vc1 = FirstViewController()
let vc2 = SecondViewController()
let vc3 = ThirdViewController()
viewControllers = [vc1, vc2, vc3]
}

这是我用来设置 FirstViewController 的代码:

class ProfileViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return 2
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: VCCellId, for: indexPath) as! firstVCCell

    return cell
}
}

组合 UITabBarControllerUINavigationController 时,正确的设置方法是将 UITabBarController 设为 rootViewControllerUITabBarController 的每个选项卡都有自己的 UINavigationController。因此,如果您有 4 个选项卡,您将创建 4 个 UINavigationControllers.

参见:Adding a Navigation Controller to a Tab Bar Interface


更新

根据您在更新后的问题中添加的代码,为每个 vc1vc2vc3 创建一个 UINavigationController

class CustomTabBarController: UITabBarController {
    override func viewDidLoad() {
        super.viewDidLoad()
        let vc1 = UINavigationController(rootViewController: FirstViewController())
        let vc2 = UINavigationController(rootViewController: SecondViewController())
        let vc3 = UINavigationController(rootViewController: ThirdViewController())

        viewControllers = [vc1, vc2, vc3]
    }

}

在您的每个 ViewController 中,将 title 设置为您希望在选择该选项卡时显示在导航栏中的标题:

class FirstViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .red
        title = "First"
        self.navigationController?.navigationBar.titleTextAttributes =
            [NSFontAttributeName: UIFont(name: "Chalkduster", size: 27)!,
             NSForegroundColorAttributeName: UIColor.black]
    }
}

您可能想试试这个:

viewControllers = [vc1, vc2, vc3].map{UINavigationController(rootViewController: [=10=])}

我在已接受答案的评论中注意到您想知道在哪里更改导航栏的属性。如果您计划对每个选项卡应用相同的自定义,您可以在 CustomTabBarController 中使用以下代码:

class CustomTabBarController: UITabBarController {
    override func viewDidLoad() {
        super.viewDidLoad()

        // Tab Bar Customisation
        tabBar.barTintColor = .systemPink
        tabBar.tintColor = .systemTeal
        tabBar.unselectedItemTintColor = .systemGray
        tabBar.isTranslucent = false

        viewControllers = [
            createTabBarItem(tabBarTitle: "Tab 1", tabBarImage: "TabBarImg1", viewController: ViewControllerOne()),
            createTabBarItem(tabBarTitle: "Tab 2", tabBarImage: "TabBarImg2", viewController: ViewControllerTwo()),
            createTabBarItem(tabBarTitle: "Tab 3", tabBarImage: "TabBarImg3", viewController: ViewControllerThree()),
            createTabBarItem(tabBarTitle: "Tab 4", tabBarImage: "TabBarImg4", viewController: ViewControllerFour())
        ]
    }

    func createTabBarItem(tabBarTitle: String, tabBarImage: String, viewController: UIViewController) -> UINavigationController {
        let navCont = UINavigationController(rootViewController: viewController)
        navCont.tabBarItem.title = tabBarTitle
        navCont.tabBarItem.image = UIImage(named: tabBarImage)

        // Nav Bar Customisation
        navCont.navigationBar.barTintColor = .systemRed
        navCont.navigationBar.tintColor = .systemBlue
        navCont.navigationBar.isTranslucent = false
        return navCont
    }
}

就我个人而言,我喜欢这种方法,因为它使我免于在整个 ViewController 中重复代码,并使选项卡和导航条码在您的自定义选项卡栏中井然有序 class。

如果您不想在整个应用程序中使用相同的选项卡或导航栏自定义,您可以在各自的 ViewControllers class 中根据自己的喜好简单地单独自定义每一个。但是,如果每个选项卡都有不同的选项卡 and/or nav attributes Vacawama 的答案很可能是最好的方法!