UINavigationBar Text Color in Swift

点点圈 提交于 2019-12-03 01:49:01

问题


How would I go about changing the color of the UINavigationBar in Swift?

Most things online say to do something like:

[self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];

Which I translated to

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

But it doesn't work. I already changed the background and button colors, but the text color doesn't change. Any ideas?


回答1:


Use NSForegroundColorAttributeName as key, not "NSForegroundColorAttributeName" string.

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



回答2:


You can also change all UINavigationController appearances in your app within the AppDelegate.swift file. Just put the following code within the application:didFinishLaunchingWithOptions function:

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

Creds: Coderwall's Blog Post




回答3:


Swift 3+

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

Swift 4.0

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



回答4:


Swift 2.0

self.navigationController!.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName : UIColor.whiteColor()]



回答5:


    //Nav Bar Title
    self.title = "WORK ORDER"
    self.navigationController!.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName : UIColor.whiteColor()]



回答6:


Swift 3

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



回答7:


I use like:

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

     return true
   }



回答8:


Swift 4.x:

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



回答9:


Swift 4.2

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



回答10:


    let titleDict = [NSForegroundColorAttributeName: UIColor.white]
    self.navigationController?.navigationBar.titleTextAttributes = titleDict



回答11:


Swift 5.1:

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


来源:https://stackoverflow.com/questions/24402000/uinavigationbar-text-color-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!