简体   繁体   English

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

[英]UINavigationController, and TabBarController programmatically (no storyboards)

Currently, my AppDelegate file contains this code to establish the CustomTabBarController as the rootViewController:目前,我的 AppDelegate 文件包含以下代码,用于将 CustomTabBarController 建立为 rootViewController:

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

I want my app to always have the CustomTabBarController on the bottom, but I want each tab to have a navigation controller.我希望我的应用程序始终在底部有 CustomTabBarController,但我希望每个选项卡都有一个导航控制器。 Here is the code I used to set up my tabBarController:这是我用来设置我的 tabBarController 的代码:

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

Here is the code I used to set up my FirstViewController:这是我用来设置 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
}
}

When combining a UITabBarController and UINavigationController s, the correct way to set that up is to make the UITabBarController the rootViewController .当组合UITabBarControllerUINavigationController ,正确的设置方法是让UITabBarController成为rootViewController Each tab of your UITabBarController gets its own UINavigationController .您的UITabBarController每个选项卡都有自己的UINavigationController So, if you have 4 tabs, you will create 4 UINavigationControllers .因此,如果您有 4 个选项卡,您将创建 4 个UINavigationControllers

See: Adding a Navigation Controller to a Tab Bar Interface请参阅: 将导航控制器添加到选项卡栏界面


Update更新

Building off of the code you added in your updated question, create a UINavigationController for each of your vc1 , vc2 , and vc3 .你在你的更新问题添加的代码的建立了,创造UINavigationController为每个的vc1vc2vc3

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

}

In each of your ViewController s, set title to the title you want to be displayed in the navigation bar when that tab is selected:在您的每个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]
    }
}

I noticed in the comments of the accepted answer that you were wondering where to change the attributes of the navigation bar.我在接受的答案的评论中注意到您想知道在哪里更改导航栏的属性。 If you plan on applying the same customization to each tab you could use the following code within your CustomTabBarController:如果您计划对每个选项卡应用相同的自定义,您可以在 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
    }
}

Personally, I like this approach, because it saves me from repeating code throughout ViewControllers and keeps the tab & nav bar code organized within your custom tab bar class.就我个人而言,我喜欢这种方法,因为它使我免于在整个 ViewController 中重复代码,并在您的自定义标签栏类中组织标签和导航栏代码。

If you don't want to use the same tab or nav bar customization throughout the app you can simply customize each one individually how you like within their respective ViewControllers class.如果您不想在整个应用程序中使用相同的选项卡或导航栏自定义,您可以简单地在各自的 ViewControllers 类中单独自定义每个选项卡或导航栏。 However, if every tab is going to have different tab and/or nav attributes Vacawama's answer would most likely be the best approach!但是,如果每个标签都有不同的标签和/或导航属性,那么 Vacawama 的答案很可能是最好的方法!

你可能想试试这个:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM