【问题标题】:Navigation Bar Title Missing缺少导航栏标题
【发布时间】:2021-11-01 22:07:22
【问题描述】:

呈现的每个视图控制器都需要呈现自己的导航栏标题 - 我正在以编程方式创建标题 - 但是如何将它们传递给类似于 viewControllers 数组的 CBFlashyTabBarController 函数。目前导航栏控制器完全没有标题。

let test1VC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "Test1")

执行 pushViewController 后,不会显示以下 navigationItem.title。

  test1VC.navigationItem.title = "Test1"
  test1VC.tabBarItem = UITabBarItem(title: "Test1", image: #imageLiteral(resourceName: "img1TabLarge"), tag: 0)
                            
let test2VC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "0")

执行 pushViewController 后,不会显示以下 navigationItem.title。

  test1VC.navigationItem.title = "Test2"
  test1VC.tabBarItem = UITabBarItem(title: "Test2", image: #imageLiteral(resourceName: "img2TabLarge"), tag: 0)

let tabBarController = CBFlashyTabBarController()

let controllers = [test1VC, test2VC]
  tabBarController.viewControllers = controllers
                        
navigationController?.pushViewController(tabBarController, animated: true)

【问题讨论】:

  • 标题是viewController的属性,不是navigationItem。你放错地方了。
  • 你能告诉我一个如何为每个viewController分配导航标题的例子吗?

标签: swift


【解决方案1】:

您没有创建 UINavigationController 这就是原因。在使用 TabBarController 时,最好为每个选项卡使用单独的 NavigationController,而不是推送 viewController,而是将 tabBarController 呈现在当前 VC 上方。

let test1VC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "Test1")
        
test1VC.navigationItem.title = "Test1"
test1VC.tabBarItem = UITabBarItem(title: "Test1", image: #imageLiteral(resourceName: "img1TabLarge"), tag: 0)
    
let test2VC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "0")
    
let controller1 = UINavigationController(rootViewController: test1VC)
let controller2 = UINavigationController(rootViewController: test2VC)
    
let tabBarController = CBFlashyTabBarController()

let controllers = [controller1, controller2]
    tabBarController.viewControllers = controllers
    
tabBarController.modalPresentationStyle = .fullScreen
    
self.present(tabBarController, animated: true, completion: nil)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-21
    • 1970-01-01
    相关资源
    最近更新 更多