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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43961766/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 09:58:11  来源:igfitidea点击:

UINavigationController, and TabBarController programmatically (no storyboards)

iosswiftxcodeswift3xcode8

提问by Big Johnson

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. Here is the code I used to set up my tabBarController:

我希望我的应用程序始终在底部有 CustomTabBarController,但我希望每个选项卡都有一个导航控制器。这是我用来设置我的 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
}
}

回答by vacawama

When combining a UITabBarControllerand UINavigationControllers, the correct way to set that up is to make the UITabBarControllerthe rootViewController. Each tab of your UITabBarControllergets its own UINavigationController. So, if you have 4 tabs, you will create 4 UINavigationControllers.

组合 aUITabBarControllerUINavigationControllers 时,正确的设置方法是UITabBarControllerrootViewController. 您的每个标签UITabBarController都有自己的UINavigationController. 因此,如果您有 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 UINavigationControllerfor 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 ViewControllers, set titleto the title you want to be displayed in the navigation bar when that tab is selected:

在您ViewController的每个s 中,设置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]
    }
}

回答by Cooparr

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. However, if every tab is going to have different tab and/or nav attributes Vacawama's answer would most likely be the best approach!

如果您不想在整个应用程序中使用相同的选项卡或导航栏自定义,您可以简单地在各自的 ViewControllers 类中单独自定义每个选项卡或导航栏。但是,如果每个选项卡都有不同的选项卡和/或导航属性,那么 Vacawama 的答案很可能是最好的方法!

回答by mikeeategg

You may want to try this:

你可能想试试这个:

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