【问题标题】:NavigationBar bar, tint, and title text color in iOS 8iOS 8 中的 NavigationBar 栏、色调和标题文本颜色
【发布时间】:2014-11-18 10:52:21
【问题描述】:

状态栏中的背景文本仍然是黑色的。如何将颜色更改为白色?

// io8, swift, Xcode 6.0.1 
override func viewDidLoad() {
    super.viewDidLoad()
    self.navigationController?.navigationBar.barTintColor = UIColor.blackColor()
    self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.orangeColor()]

}

【问题讨论】:

    标签: ios swift xcode ios8 uinavigationbar


    【解决方案1】:

    要普遍更改颜色,此代码应位于NavigationControllerviewDidLoad 函数中:

    class NavigationController: UINavigationController, UIViewControllerTransitioningDelegate {
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            // Status bar white font
            self.navigationBar.barStyle = UIBarStyle.Black
            self.navigationBar.tintColor = UIColor.whiteColor()
        }
    }
    

    要根据ViewController 更改它,您必须从ViewController 中引用NavigationController,并在ViewControllerviewWillAppear 函数中编写类似的行。

    【讨论】:

    • 如何覆盖 NavigationController 以使用这些方法?
    • 你不要重写 NavigationController,你只是在 ViewDidLoad 函数中修改它。我更新了上面的答案。
    • 所以开发人员只需将 NavigationController.swift 添加到工作区?如果是这样,那就太好了!
    • @AG1 这是不正确的,仅添加一个新文件 NavigationController.swift 是不够的,您必须将 Storyboard 中的 Navigation Controller 连接到 Identity Inspector 中的此文件,因为默认情况下它将使用一个通用的 UINavigationController。
    • 您也可以在情节提要中完成所有这些操作,而无需创建自定义导航控制器。 Bar Tine 和 Style 的属性就在属性检查器的右侧。
    【解决方案2】:

    我喜欢亚历克斯的回答。如果您想在ViewController 中快速尝试一些东西,请确保使用

    viewWillAppear()
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        var nav = self.navigationController?.navigationBar
        nav?.barStyle = UIBarStyle.Black
        nav?.tintColor = UIColor.white
        nav?.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.orange]
        //nav?.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.orange] // swift 4.2
    }
    

    【讨论】:

    • 但我使用的是 iso 8.1
    • 为什么是 viewWillAppear()?我在 viewDidLoad 中执行此操作,效果很好。
    • 因为每次查看 UIView 时都希望设置它。如果你在 viewDidLoad() 中设置,比如在 UITabBar 视图中,移动到另一个选项卡再次设置它,然后回来它会被覆盖,因为原始视图只加载了一次。
    • 更新 swift 4 - NSForegroundColorAttributeName 现在应该是 NSAttributedStringKey.foregroundColor
    • Swift 4.2 更新:NSAttributedString.Key.foregroundColor 而不是 NSForegroundColorAttributeName
    【解决方案3】:

    AppDelegate.swift 中,在 application(_:didFinishLaunchingWithOptions:) 我输入以下内容:

    UINavigationBar.appearance().barTintColor = UIColor(red: 234.0/255.0, green: 46.0/255.0, blue: 73.0/255.0, alpha: 1.0)
    UINavigationBar.appearance().tintColor = UIColor.white
    UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor.white]
    

    (对于 Swift 4 或更早版本,请使用 NSAttributedStringKey 而不是 NSAttributedString.Key

    对于titleTextAttributesdocs 说:

    您可以指定字体、文本颜色、文本阴影颜色和文本 文本属性字典中标题的阴影偏移量

    【讨论】:

    • 如果我们有多个导航栏怎么办?
    • tintColor 不适用于 Xcode 7.3.1。文字仍然是黑色的。
    • 在 Xcode 8.2 中,titleTextAttributes 不会自动完成,但它可以工作。
    • 还需要 barTintColor 吗?删除该行仍然有效
    • 这是添加一个文本属性。实际上并没有改变标题的颜色。如果您注意文本的字体,您就可以理解其中的区别。
    【解决方案4】:

    要在 objective-c 中工作,我必须在 CustomViewController 的 viewWillAppear 中添加以下几行。

    [self.navigationController.navigationBar setBarTintColor:[UIColor whiteColor]];
    [self.navigationController.navigationBar setTranslucent:NO];
    

    对于 Swift2.x 这有效:

    self.navigationController?.navigationBar.barTintColor = UIColor.redColor()
    

    对于 Swift3.x 这有效:

    self.navigationController?.navigationBar.barTintColor = UIColor.red
    

    【讨论】:

    • 但是当我回到另一个视图控制器时,颜色也发生了变化。关于如何防止这种情况的任何想法。我希望我的主视图控制器是一种颜色,而另一种是另一种颜色。
    • 我认为您还必须在新视图控制器中设置颜色。
    【解决方案5】:

    用 swift 4 更新

    override func viewDidLoad() {
        super.viewDidLoad()
            self.navigationController?.navigationBar.tintColor = UIColor.blue
            self.navigationController?.navigationBar.barStyle = UIBarStyle.black
    }
    

    【讨论】:

      【解决方案6】:

      如果要设置整个app的tint color和bar color,可以在AppDelegate.swift中添加如下代码

       func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
              // Override point for customization after application launch.
      
          var navigationBarAppearace = UINavigationBar.appearance()
      
          navigationBarAppearace.tintColor = UIColor(red:1.00, green:1.00, blue:1.00, alpha:1.0)
          navigationBarAppearace.barTintColor = UIColor(red:0.76, green:0.40, blue:0.40, alpha:1.0)
          navigationBarAppearace.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
          return true
      `
      

      Navigation barTintColor and tintColor is set

      【讨论】:

        【解决方案7】:

        在故事板中完成这项工作(Interface Builder Inspector)

        IBDesignable 的帮助下,我们可以为UINavigationController 添加更多选项到Interface Builder Inspector 并在故事板上调整它们。首先,将以下代码添加到您的项目中。

        @IBDesignable extension UINavigationController {
            @IBInspectable var barTintColor: UIColor? {
                set {
                    navigationBar.barTintColor = newValue
                }
                get {
                    guard  let color = navigationBar.barTintColor else { return nil }
                    return color
                }
            }
        
            @IBInspectable var tintColor: UIColor? {
                set {
                    navigationBar.tintColor = newValue
                }
                get {
                    guard  let color = navigationBar.tintColor else { return nil }
                    return color
                }
            }
        
            @IBInspectable var titleColor: UIColor? {
                set {
                    guard let color = newValue else { return }
                    navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: color]
                }
                get {
                    return navigationBar.titleTextAttributes?["NSForegroundColorAttributeName"] as? UIColor
                }
            }
        }
        

        然后简单地在 storyboard 上设置 UINavigationController 的属性。

        【讨论】:

          【解决方案8】:

          对于TitleText NavigationBar 的自定义颜色,这里有一个简单而简短的 Swift 3 代码:

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

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

          【讨论】:

            【解决方案9】:

            斯威夫特 5

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

            斯威夫特 4

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

            【讨论】:

            • 我得到:类型'NSAttributedStringKey'(又名'NSString')没有成员'foregroundColor'
            • 请使用以下代码获取最新版本的 xcode 10 和 swift 4.2 self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
            【解决方案10】:

            斯威夫特 4

            override func viewDidLoad() {
                super.viewDidLoad()
            
                navigationController?.navigationBar.barTintColor = UIColor.orange
                navigationController?.navigationBar.tintColor = UIColor.white
                navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
            }
            

            【讨论】:

              【解决方案11】:

              斯威夫特 4.1

              添加一个函数到 viewDidLoad

              override func viewDidLoad() {
                super.viewDidLoad()
              
                setup()
              }   
              

              setup()函数中添加:

              func setup() {
              
                      navigationController?.navigationBar.prefersLargeTitles = true
                      navigationController?.navigationBar.barStyle = .blackOpaque
                      navigationItem.title = "YOUR_TITLE_HERE"
                      navigationController?.navigationBar.barTintColor = .black
                      let attributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
                      navigationController?.navigationBar.largeTitleTextAttributes = attributes
                  }
              

              【讨论】:

                【解决方案12】:

                在 Swift 3 中这有效:

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

                【讨论】:

                  【解决方案13】:

                  在 Swift5 和 Xcode 10 中

                  self.navigationItem.title = "your name"
                  let textAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
                  navigationController?.navigationBar.titleTextAttributes = textAttributes
                  

                  【讨论】:

                  • 虽然这段代码 sn-p 可以解决问题,但including an explanation 确实有助于提高帖子的质量。请记住,您是在为将来的读者回答问题,而这些人可能不知道您提出代码建议的原因。
                  【解决方案14】:

                  在 Swift 4.2 中

                  var nav = self.navigationController?.navigationBar
                  nav?.barStyle = UIBarStyle.Black
                  nav?.tintColor = UIColor.white
                  nav?.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.orange]
                  

                  【讨论】:

                    【解决方案15】:

                    Swift 4.2 版本的 Albert 的回答-

                    UINavigationBar.appearance().barTintColor = UIColor(red: 234.0/255.0, green: 46.0/255.0, blue: 73.0/255.0, alpha: 1.0)
                    UINavigationBar.appearance().tintColor = UIColor.white
                    UINavigationBar.appearance().titleTextAttributes = [.foregroundColor : UIColor.white]
                    

                    【讨论】:

                    • 我真的很喜欢你添加这个答案的方式。 .barTintColor 现在不是一个选项。你是说 .backgroundColor 吗?
                    【解决方案16】:

                    在 Swift 4.2 版本中将导航栏标题的文本颜色设置为白色:

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

                    【讨论】:

                      【解决方案17】:

                      Swift 到 Swift 3.2(不是 Swift 4.0)

                          self.navigationController?.navigationItem.largeTitleDisplayMode = .always
                          self.navigationController?.navigationBar.prefersLargeTitles = true
                          self.navigationController?.navigationBar.largeTitleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
                      
                          // unconfirmed but I assume this works:
                          self.navigationController?.navigationBar.barTintColor = UIColor.white
                          self.navigationController?.navigationBar.barStyle = UIBarStyle.black
                      

                      【讨论】:

                        【解决方案18】:

                        斯威夫特 5.1

                        仅复制和粘贴ViewDidLoad() 并根据需要更改其大小。 在复制和粘贴之前,在屏幕顶部添加导航栏。

                        navigationController?.navigationBar.titleTextAttributes = [ NSAttributedString.Key.font: UIFont(name: "TitilliumWeb-Bold.ttf", size: 16.0)!, NSAttributedString.Key.foregroundColor: UIColor.white]
                        
                        

                        如果它不起作用,那么您可以尝试仅更改其文本颜色

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

                        【讨论】:

                          猜你喜欢
                          • 2014-11-27
                          • 2014-05-05
                          • 1970-01-01
                          • 1970-01-01
                          • 2015-07-13
                          • 1970-01-01
                          • 2023-03-03
                          • 2012-03-28
                          • 1970-01-01
                          相关资源
                          最近更新 更多