本文介绍了UINavigationController和TabBarController以编程方式(没有情节提要)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我的AppDelegate文件包含以下代码以将CustomTabBarController建立为rootViewController:

Currently, my AppDelegate file contains this code to establish the CustomTabBarController as the rootViewController:

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

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

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:

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

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

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

推荐答案

结合使用 UITabBarController UINavigationController s时,正确的设置方法是 UITabBarController rootViewController .您的 UITabBarController 的每个标签都具有自己的 UINavigationController .因此,如果您有4个标签,则将创建4个 UINavigationControllers .

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

请参阅:将导航控制器添加到标签栏界面

更新

以您在更新的问题中添加的代码为基础,为您的每个 vc1 vc2 创建一个 UINavigationController > vc3 .

Building off of the code you added in your updated question, create a UINavigationController for each of your vc1, vc2, and vc3.

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 设置为要在选择该选项卡时显示在导航栏中的标题:

In each of your ViewControllers, set title to the title you want to be displayed in the navigation bar when that tab is selected:

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

这篇关于UINavigationController和TabBarController以编程方式(没有情节提要)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 08:35