【问题标题】:UINavigationController, and TabBarController programmatically (no storyboards)UINavigationController 和 TabBarController 以编程方式(无情节提要)
【发布时间】:2017-05-14 08:12:21
【问题描述】:

目前,我的 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
}
}

【问题讨论】:

  • 你看到导航栏了吗?
  • 从你的问题开始,你正在寻找 tabbarcontroller 中的导航控制器。如果是,请查看此链接iosinsight.com/… 如果您正在以编程方式添加 //Initialize tabbar Controller 和 All Navigation Controllers are added into Tabbar Controller self.tabbar = [[UITabBarController alloc] init]; NSArray *arrNVControllers = [NSArray arrayWithObjects:nav1,nav2,nav3,nav4, nil]; self.tabbar.viewControllers = arrNVControllers;
  • CustomTabBarController的实现是什么?您需要在每个视图控制器周围手动添加导航控制器
  • @Alistra 我该怎么做?我尝试将我的 CustomTabBarController 的第一个子类子类化为 UINavigationController,但是当我运行它时没有导航栏。
  • @Md.Ib 是的,我看到了一个导航栏

标签: ios swift xcode swift3 xcode8


【解决方案1】:

当组合UITabBarControllerUINavigationControllers 时,正确的设置方法是将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]
    }

}

在您的每个ViewControllers 中,将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]
    }
}

【讨论】:

  • 您拥有的链接上的代码已经过时了,但是我尝试这样做:let profileNavBar = UINavigationController(rootViewController: FirstViewController()),但是当我构建应用程序时,它崩溃了。其中 FirstViewController 是我的 customtabbar 中的第一个 ViewController。
  • 使用创建 UICustomTabBarController 的代码更新您的答案,并显示创建视图控制器并将它们添加到 UICustomTabBarController 的代码。
  • 更新了创建 UICUstomTabBarController 和 FirstViewController 的代码。
  • 这行得通,但是我将如何更改 nabber 的属性(如字体)?我必须在哪个文件中更改它,执行此操作的代码是什么?
  • 什么是nabber?
【解决方案2】:

我在接受的答案的 cmets 中注意到您想知道在哪里更改导航栏的属性。如果您计划对每个选项卡应用相同的自定义,您可以在 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
    }
}

就我个人而言,我喜欢这种方法,因为它使我免于在整个 ViewControllers 中重复代码,并使标签和导航条码在您的自定义标签栏类中组织起来。

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

【讨论】:

    【解决方案3】:

    你可能想试试这个:

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

    【讨论】:

    • 这会出现以下错误:由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“不支持推送导航控制器”
    猜你喜欢
    • 2010-12-28
    • 1970-01-01
    • 2012-10-13
    • 2012-02-12
    • 1970-01-01
    • 2012-05-11
    • 2012-12-31
    • 2017-08-11
    • 2015-06-12
    相关资源
    最近更新 更多