簡體   English   中英

導航 Controller 在使用 TabBar 控制器時不顯示標題(以編程方式)

[英]Navigation Controller doesn't show the title while working with TabBar Controller(Programmatically)

我使用navigationController作為window.rootViewController 作為navigationControllerrootViewController ,我使用的是 tabBarController。 但是當我運行應用程序時,navigationBar 的標題沒有出現(我的 Tabbar Controller 出現了,我可以看到我的 viewController。沒問題) 我嘗試使用 viewDidload 中的以下兩個代碼在 tabBar 的每個 viewController 中命名 navigationBar,但仍然沒有任何改變。

 navigationController?.title = "title"
 navigationController?.navigationBar.topItem?.title = "title"

我在 SceneDelegete 中設置了帶有標簽欄的 navigationController,如下所示:

let countriesTabbarController = TabBarControllerBuilder.build()
    
    let navigationController = UINavigationController(rootViewController: countriesTabbarController)
    
    window?.rootViewController = navigationController
    window?.makeKeyAndVisible()

如何為 tabBar 的每個 viewController 顯示我的 navigationController 標題?

每個視圖 controller 都有一個名為navigationItem類型為UINavigationItem的屬性。 如果您設置它的title ,它會在導航欄中顯示(如果存在)。

例如在viewDidLoad中,添加:

self.navigationItem.title = "title"

換句話說:標題是在導航 controller 堆棧中包含的視圖 controller 上設置的,而不是導航 controller 本身。 也沒有在導航欄上,這可能也是我現在想不起來的一個很好的理由。

https://developer.apple.com/documentation/uikit/uinavigationitem

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM