Text UINavigationBar文本颜色在Swift中

Text UINavigationBar文本颜色在Swift中,text,colors,navigation,uinavigationbar,swift,Text,Colors,Navigation,Uinavigationbar,Swift,如何在Swift中更改UINavigationBar的颜色 大多数在线活动都会说: [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}]; 我翻译成 let titleDict: NSDictionary = ["NSForegroundColorAttributeName": UIColor.whiteC

如何在Swift中更改
UINavigationBar
的颜色

大多数在线活动都会说:

[self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];
我翻译成

let titleDict: NSDictionary = ["NSForegroundColorAttributeName": UIColor.whiteColor()]
self.navigationController.navigationBartitleTextAttributes = titleDict
//self is referring to a UIViewController

但它不起作用。我已经改变了背景和按钮的颜色,但是文本的颜色没有改变。有什么想法吗?

使用
NSForegroundColorAttributeName
作为键,而不是
“NSForegroundColorAttributeName”
字符串

let titleDict: NSDictionary = [NSForegroundColorAttributeName: UIColor.whiteColor()]
self.navigationController.navigationBar.titleTextAttributes = titleDict

您还可以在
AppDelegate.swift
文件中更改应用程序中的所有
UINavigationController
外观。只需将以下代码放入
应用程序:didFinishLaunchingWithOptions
函数:

var navigationBarAppearace = UINavigationBar.appearance()

navigationBarAppearace.tintColor = UIColor.YourNavigationButtonsColor()  // Back buttons and such
navigationBarAppearace.barTintColor = UIColor.YourBackgroundColor()  // Bar's background color

navigationBarAppearace.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.YourTitleColor()]  // Title's text color

信誉:Swift 2.0

self.navigationController!.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName : UIColor.whiteColor()]
self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor : UIColor.white]

Swift 3+

self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName : UIColor.white]
Swift 4.0

self.navigationController!.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName : UIColor.whiteColor()]
self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor : UIColor.white]
我喜欢:

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
      let navigationBarAppearace = UINavigationBar.appearance()
      navigationBarAppearace.titleTextAttributes = [NSForegroundColorAttributeName : UIColor.whiteColor()]

     return true
   }

Swift 3

UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.white], for: .selected)
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.black], for: .normal)
Swift 4.x:

UINavigationBar.appearance().titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]

Swift 4.2

self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]

Swift 5.1:

    let titleDict: NSDictionary = [NSAttributedString.Key.foregroundColor: UIColor.white]
    navigationController?.navigationBar.titleTextAttributes = titleDict as? [NSAttributedString.Key : Any]

根据Swift 1.2更新,语法需要如下
let titleDict:NSDictionary=[NSForegroundColorAttributeName:UIColor.whiteColor()]self.navigationController!。navigationBar.titleTextAttributes=标题ICT为[NSObject:AnyObject]
根据Swift 2.2:
让标题ICT:NSDictionary=[NSForegroundColorAttributeName:UIColor.whiteColor()]self.navigationController!。navigationBar.titleTextAttributes=标题为?[String:AnyObject]
有效,谢谢!令人难以置信的可怕,虽然你不能简单地设置一个出口或风格在IB。。。我发现导航控制器很难定制,所以我最终制作了自己的顶栏:/谢谢!工作正常。感谢您在色调/条形色调旁边发表评论,这帮助我意识到我做错了什么,从而结束了我的头撞墙循环D