UINavigationBar Text Color in Swift

前端 未结 11 1652
刺人心
刺人心 2021-01-30 12:20

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

Most things online say to do something like:

[self.navigationContro         


        
相关标签:
11条回答
  • 2021-01-30 12:57

    Swift 5.1:

        let titleDict: NSDictionary = [NSAttributedString.Key.foregroundColor: UIColor.white]
        navigationController?.navigationBar.titleTextAttributes = titleDict as? [NSAttributedString.Key : Any]
    
    0 讨论(0)
  • 2021-01-30 13:00

    Swift 3+

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

    Swift 4.0

    self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor : UIColor.white]
    
    0 讨论(0)
  • 2021-01-30 13:05
        let titleDict = [NSForegroundColorAttributeName: UIColor.white]
        self.navigationController?.navigationBar.titleTextAttributes = titleDict
    
    0 讨论(0)
  • 2021-01-30 13:15

    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

    0 讨论(0)
  • 2021-01-30 13:16

    I use like:

        func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
          let navigationBarAppearace = UINavigationBar.appearance()
          navigationBarAppearace.titleTextAttributes = [NSForegroundColorAttributeName : UIColor.whiteColor()]
    
         return true
       }
    
    0 讨论(0)
  • 2021-01-30 13:16

    Swift 4.2

    self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
    
    0 讨论(0)
提交回复
热议问题