【问题标题】:UINavigationBar Text Color in SwiftSwift 中的 UINavigationBar 文本颜色
【发布时间】:2014-08-15 14:56:48
【问题描述】:

我将如何在 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

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

【问题讨论】:

  • 谢谢!效果很好。

标签: text colors navigation uinavigationbar swift


【解决方案1】:

使用NSForegroundColorAttributeName 作为键,而不是"NSForegroundColorAttributeName" 字符串。

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

【讨论】:

  • 根据 Swift 1.2 更新,语法必须是 let titleDict: NSDictionary = [NSForegroundColorAttributeName: UIColor.whiteColor()] self.navigationController!.navigationBar.titleTextAttributes = titleDict as [NSObject : AnyObject]
  • 根据 Swift 2.2:let titleDict: NSDictionary = [NSForegroundColorAttributeName: UIColor.whiteColor()] self.navigationController!.navigationBar.titleTextAttributes = titleDict as? [String : AnyObject]
【解决方案2】:

您还可以在 AppDelegate.swift 文件中更改应用中所有 UINavigationController 的外观。只需将以下代码放入application: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

信用:Coderwall's Blog Post

【讨论】:

  • 感谢您在 tint / bar tint color 旁边放置 cmets,这帮助我意识到我做错了什么,从而结束了我的头撞墙循环。 :D
【解决方案3】:

Swift 3+

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

Swift 4.0

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

【讨论】:

  • 工作,谢谢!令人难以置信的可怕,尽管你不能简单地在 IB 中设置一个插座或样式......我发现导航控制器很难定制,我最终制作了自己的顶栏:/
【解决方案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】:

      斯威夫特 3

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

      【讨论】:

        【解决方案7】:

        Swift 4.x:

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

        【讨论】:

          【解决方案8】:

          斯威夫特 4.2

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

          【讨论】:

            【解决方案9】:

            我用喜欢:

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

            【讨论】:

              【解决方案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]
                

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 2018-10-27
                  • 2013-04-11
                  • 1970-01-01
                  • 1970-01-01
                  • 2014-11-21
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  相关资源
                  最近更新 更多