【问题标题】:Changing navigation bar color in Swift在 Swift 中更改导航栏颜色
【发布时间】:2014-09-01 11:02:02
【问题描述】:

我正在使用 Picker View 来允许用户为整个应用选择颜色主题。

我正计划更改导航栏、背景以及可能的标签栏的颜色(如果可能的话)。

我一直在研究如何做到这一点,但找不到任何 Swift 示例。谁能给我一个我需要用来更改导航栏颜色和导航栏文本颜色的代码示例?

Picker View 设置好了,我正在寻找更改 UI 颜色的代码。

【问题讨论】:

    标签: ios swift uinavigationbar


    【解决方案1】:

    导航栏:

    navigationController?.navigationBar.barTintColor = UIColor.green
    

    将 greenColor 替换为您想要的任何 UIColor,如果您愿意,也可以使用 RGB。

    导航栏文字:

    navigationController?.navigationBar.titleTextAttributes = [.foregroundColor: UIColor.orange]
    

    将 orangeColor 替换为您喜欢的任何颜色。

    标签栏:

    tabBarController?.tabBar.barTintColor = UIColor.brown
    

    标签栏文本:

    tabBarController?.tabBar.tintColor = UIColor.yellow
    

    在最后两个中,将 brownColor 和 yellowColor 替换为您选择的颜色。

    【讨论】:

    • 非常感谢!我的尝试与我的尝试相差不远,但我没有完全按照正确的顺序进行操作。
    • 我不确定。如果您使用的是推送转场而不是模式,它应该是相同的导航栏,但我不完全确定。对不起。
    • 更新到较新的 Xcode 测试版后,设置标题文本颜色不再有效。 titleTextAttributes 在 Swift 中不可用。有什么想法吗?
    • 你能打开一个新问题并可能链接到它吗?聊天不是处理此类事情的最佳场所。
    • 我发现它让我使用 NSForegroundColorAttributeName 作为属性名称,但在其他方面效果很好。
    【解决方案2】:

    以下是一些非常基本的外观自定义,您可以在应用范围内应用:

    UINavigationBar.appearance().backgroundColor = UIColor.greenColor()
    UIBarButtonItem.appearance().tintColor = UIColor.magentaColor()
    //Since iOS 7.0 UITextAttributeTextColor was replaced by NSForegroundColorAttributeName
    UINavigationBar.appearance().titleTextAttributes = [UITextAttributeTextColor: UIColor.blueColor()]
    UITabBar.appearance().backgroundColor = UIColor.yellowColor();
    

    斯威夫特 5.4.2:

    UINavigationBar.appearance().backgroundColor = .green // backgorund color with gradient
    // or
    UINavigationBar.appearance().barTintColor = .green  // solid color
        
    UIBarButtonItem.appearance().tintColor = .magenta
    UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor.blue]
    UITabBar.appearance().barTintColor = .yellow
    

    更多关于UIAppearance Swift API 你可以阅读here

    【讨论】:

    • 那么我将如何使用它来更改整个应用程序导航栏的颜色?目前我只有: self.navigationController.navigationBar.barTintColor = UIColor.newBlueColor() 当然这只是改变了代码所在的视图控制器导航栏的颜色。如何使用它来更改所有导航栏?我尝试使用: UINavigationBar.appearance().backgroundColor = UIColor.newBlueColor() 但它似乎没有做任何事情。
    • 要反映整个应用程序的变化,请将上面的内容粘贴到 AppDelegate.swift func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { //Place以上代码}
    • 使用 barTintColor 代替 backgroundColor。 UINavigationBar.appearance().barTintColor = UIColor.greenColor()
    • @Keenle 我有点困惑...为什么通过外观 API 更改 UINavigationBar 的背景颜色不会完全改变其颜色?我试图将背景颜色设置为蓝色,它给了我一种奇怪的紫蓝色阴影......
    【解决方案3】:

    为 Swift 3、4、4.2、5+ 更新

    // setup navBar.....
    UINavigationBar.appearance().barTintColor = .black
    UINavigationBar.appearance().tintColor = .white
    UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
    UINavigationBar.appearance().isTranslucent = false
    

    斯威夫特 4

    UINavigationBar.appearance().barTintColor = .black
    UINavigationBar.appearance().tintColor = .white
    UINavigationBar.appearance().titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white]
    UINavigationBar.appearance().isTranslucent = false
    

    Swift 4.2、5+

    UINavigationBar.appearance().barTintColor = .black
    UINavigationBar.appearance().tintColor = .white
    UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
    UINavigationBar.appearance().isTranslucent = false
    

    如果您想使用大标题,请添加以下行:

    UINavigationBar.navigationBar.prefersLargeTitles = true
    

    也可以在这里查看:https://github.com/hasnine/iOSUtilitiesSource

    【讨论】:

    • Swift 4.2:NSAttributedString.Key.foregroundColor
    • 将色调颜色设置为白色而不是 bartintcolor 会显示原始颜色。太好了!
    • @NickCoder 感激不尽。 :) 还要检查我的库:github.com/hasnine/iOSUtilitiesSource
    • @Markus 哦,伤心!再试一次兄弟。
    • 大标题后无效UINavigationBar.appearance().prefersLargeTitles = true,请问如何解决?
    【解决方案4】:
    UINavigationBar.appearance().barTintColor = UIColor(red: 46.0/255.0, green: 14.0/255.0, blue: 74.0/255.0, alpha: 1.0)
    UINavigationBar.appearance().tintColor = UIColor.whiteColor()
    UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName : UIColor.whiteColor()]
    

    只需将此行粘贴到您的代码中的didFinishLaunchingWithOptions

    【讨论】:

    • 我用 RGB 试过这个,不管放在哪里都行不通。
    • @NathanMcKaskle 检查你的 RGB,它应该是 "xx/250.0f" 格式。
    • 在 didFinishLaunchingWithOptions 中使用并完美运行。内部 viewDidLoad 不能完美运行。
    【解决方案5】:

    AppDelegate 中,这已全局更改了 NavBar 的格式并删除了底线/边框(这是大多数人的问题区域),以提供我认为您和其他人正在寻找的内容为:

     func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    
        UINavigationBar.appearance().setBackgroundImage(UIImage(), forBarPosition: UIBarPosition.Any, barMetrics: UIBarMetrics.Default)
        UINavigationBar.appearance().shadowImage = UIImage()
        UINavigationBar.appearance().tintColor = UIColor.whiteColor()
        UINavigationBar.appearance().barTintColor = Style.SELECTED_COLOR
        UINavigationBar.appearance().translucent = false
        UINavigationBar.appearance().clipsToBounds = false
        UINavigationBar.appearance().backgroundColor = Style.SELECTED_COLOR
        UINavigationBar.appearance().titleTextAttributes = [NSFontAttributeName : (UIFont(name: "FONT NAME", size: 18))!, NSForegroundColorAttributeName: UIColor.whiteColor()] }
    

    然后您可以设置一个 Constants.swift 文件,其中包含一个带有颜色和字体等的 Style 结构。然后您可以将 tableView/pickerView 添加到任何 ViewController 并使用“availableThemes”数组来允许用户更改主题颜色。

    关于这个的美妙之处在于,您可以在整个应用程序中为每种颜色使用一个参考,它会根据用户选择的“主题”进行更新,没有它默认为 theme1():

    import Foundation
    import UIKit
    
    struct Style {
    
    
    static let availableThemes = ["Theme 1","Theme 2","Theme 3"]
    
    static func loadTheme(){
        let defaults = NSUserDefaults.standardUserDefaults()
        if let name = defaults.stringForKey("Theme"){
            // Select the Theme
            if name == availableThemes[0]   { theme1()  }
            if name == availableThemes[1]   { theme2()  }
            if name == availableThemes[2]   { theme3()  }
        }else{
            defaults.setObject(availableThemes[0], forKey: "Theme")
            theme1()
        }
    }
    
     // Colors specific to theme - can include multiple colours here for each one
    static func theme1(){
       static var SELECTED_COLOR = UIColor(red:70/255, green: 38/255, blue: 92/255, alpha: 1) }
    
    static func theme2(){
        static var SELECTED_COLOR = UIColor(red:255/255, green: 255/255, blue: 255/255, alpha: 1) }
    
    static func theme3(){
        static var SELECTED_COLOR = UIColor(red:90/255, green: 50/255, blue: 120/255, alpha: 1) } ...
    

    【讨论】:

    • 谢谢你,你的回答真的帮助了我,至少对我来说,我使用了它的第一部分,它很棒而且非常有用
    • 非常感谢你,我在这里尝试了每一个答案,除了你的答案之外没有一个有用:D
    【解决方案6】:

    在故事板上执行此操作(Interface Builder Inspector)

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

    @IBDesignable extension UINavigationController {
        @IBInspectable var barTintColor: UIColor? {
            set {
                guard let uiColor = newValue else { return }
                navigationBar.barTintColor = uiColor
            }
            get {
                guard let color = navigationBar.barTintColor else { return nil }
                return color
            }
        }
    }
    

    然后简单地在故事板上设置导航控制器的属性。

    这种方法也可用于管理故事板中导航栏文本的颜色:

    @IBInspectable var barTextColor: UIColor? {
      set {
        guard let uiColor = newValue else {return}
        navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: uiColor]
      }
      get {
        guard let textAttributes = navigationBar.titleTextAttributes else { return nil }
        return textAttributes[NSAttributedStringKey.foregroundColor] as? UIColor
      }
    }
    

    【讨论】:

    • 爱它。虽然我认为这不适用于 OP,但它对于来自 Google 的访问者(比如我)来说是一个很好的解决方案。
    【解决方案7】:

    斯威夫特 4:

    在应用程序级别更改导航栏外观的完美工作代码。

    // MARK: Navigation Bar Customisation
    
    // To change background colour.
    UINavigationBar.appearance().barTintColor = .init(red: 23.0/255, green: 197.0/255, blue: 157.0/255, alpha: 1.0)
    
    // To change colour of tappable items.
    UINavigationBar.appearance().tintColor = .white
    
    // To apply textAttributes to title i.e. colour, font etc.
    UINavigationBar.appearance().titleTextAttributes = [.foregroundColor : UIColor.white,
                                                        .font : UIFont.init(name: "AvenirNext-DemiBold", size: 22.0)!]
    // To control navigation bar's translucency.
    UINavigationBar.appearance().isTranslucent = false
    

    编码愉快!

    【讨论】:

      【解决方案8】:
      UINavigationBar.appearance().barTintColor
      

      为我工作

      【讨论】:

        【解决方案9】:

        SWIFT 4 - 平滑过渡(最佳解决方案):

        如果您要从导航控制器返回,并且您必须在您想要使用的导航控制器上设置不同的颜色

        override func willMove(toParentViewController parent: UIViewController?) {
            navigationController?.navigationBar.barTintColor = .white
            navigationController?.navigationBar.tintColor = Constants.AppColor
        }
        

        而不是将其放在 viewWillAppear 中,以便过渡更清晰。

        SWIFT 4.2

        override func willMove(toParent parent: UIViewController?) {
            navigationController?.navigationBar.barTintColor = UIColor.black
            navigationController?.navigationBar.tintColor = UIColor.black
        }
        

        【讨论】:

          【解决方案10】:

          Swift 4

          您可以更改导航栏的颜色。只需在viewDidLoad() 中使用下面的代码 sn-p

          导航栏颜色

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

          导航栏文字颜色

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

          对于iOS 11大标题导航栏,需要使用largeTitleTextAttributes属性

          self.navigationController?.navigationBar.largeTitleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.purple]
          

          【讨论】:

            【解决方案11】:

            appearance() 函数并不总是适合我。所以我更喜欢创建一个NC对象并改变它的属性。

            var navBarColor = navigationController!.navigationBar
            navBarColor.barTintColor =
                UIColor(red:  255/255.0, green: 0/255.0, blue: 0/255.0, alpha: 100.0/100.0)
            navBarColor.titleTextAttributes =
                [NSForegroundColorAttributeName: UIColor.whiteColor()]
            

            此外,如果您想添加图像而不仅仅是文本,那也可以

            var imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 70, height: 70))
            imageView.contentMode = .ScaleAspectFit
            
            var image = UIImage(named: "logo")
            imageView.image = image
            navigationItem.titleView = imageView
            

            【讨论】:

            • 通过这种方式,我能够更改 self.navigationController?.navigationBar.topItem?.title 颜色。谢谢你。
            【解决方案12】:

            使用外观 API 和 barTintColor 颜色。

            UINavigationBar.appearance().barTintColor = UIColor.greenColor()
            

            【讨论】:

              【解决方案13】:

              Swift 5 (iOS 14)

              完整的导航栏自定义。

              // -----------------------------------------------------------
              // NAVIGATION BAR CUSTOMIZATION
              // -----------------------------------------------------------
              self.navigationController?.navigationBar.prefersLargeTitles = true
              self.navigationController?.navigationBar.tintColor = UIColor.white
              self.navigationController?.navigationBar.isTranslucent = false
              
              if #available(iOS 13.0, *) {
                  let appearance = UINavigationBarAppearance()
                  appearance.configureWithDefaultBackground()
                  appearance.backgroundColor = UIColor.blue
                  appearance.largeTitleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
                  appearance.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
              
                  navigationController?.navigationBar.standardAppearance = appearance
                  navigationController?.navigationBar.scrollEdgeAppearance = appearance
                  navigationController?.navigationBar.compactAppearance = appearance
              
              } else {
                  self.navigationController?.navigationBar.barTintColor = UIColor.blue
                  self.navigationController?.navigationBar.largeTitleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
                  self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
              }
              
              // -----------------------------------------------------------
              // NAVIGATION BAR SHADOW
              // -----------------------------------------------------------
              self.navigationController?.navigationBar.layer.masksToBounds = false
              self.navigationController?.navigationBar.layer.shadowColor = UIColor.black.cgColor
              self.navigationController?.navigationBar.layer.shadowOffset = CGSize(width: 0, height: 2)
              self.navigationController?.navigationBar.layer.shadowRadius = 15
              self.navigationController?.navigationBar.layer.shadowOpacity = 0.7
              

              【讨论】:

                【解决方案14】:

                以下代码适用于 iOS 15

                if #available(iOS 15, *) {
                        // Navigation Bar background color
                        let appearance = UINavigationBarAppearance()
                        appearance.configureWithOpaqueBackground()
                        appearance.backgroundColor = UIColor.yourColor
                        
                        // setup title font color
                        let titleAttribute = [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 25, weight: .bold), NSAttributedString.Key.foregroundColor: UIColor.yourColor]
                        appearance.titleTextAttributes = titleAttribute
                        
                        navigationController?.navigationBar.standardAppearance = appearance
                        navigationController?.navigationBar.scrollEdgeAppearance = appearance
                    }
                

                【讨论】:

                  【解决方案15】:

                  这个版本还去掉了导航栏下的1px阴影线

                  Swift 5:将其放入您的 AppDelegate didFinishLaunchingWithOptions

                  UINavigationBar.appearance().barTintColor = UIColor.black
                  UINavigationBar.appearance().tintColor = UIColor.white
                  UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
                  UINavigationBar.appearance().isTranslucent = false
                  UINavigationBar.appearance().setBackgroundImage(UIImage(), for: .any, barMetrics: .default)
                  UINavigationBar.appearance().shadowImage = UIImage()
                  

                  【讨论】:

                    【解决方案16】:

                    iOS 8 (swift)

                    let font: UIFont = UIFont(name: "fontName", size: 17)   
                    let color = UIColor.backColor()
                    self.navigationController?.navigationBar.topItem?.backBarButtonItem?.setTitleTextAttributes([NSFontAttributeName: font,NSForegroundColorAttributeName: color], forState: .Normal)
                    

                    【讨论】:

                      【解决方案17】:

                      如果你有自定义导航控制器,你可以使用上面的代码sn-p。 所以就我而言,我使用了以下代码片段。

                      Swift 3.0、XCode 8.1 版本

                      navigationController.navigationBar.barTintColor = UIColor.green
                      

                      导航栏文字:

                      navigationController.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.orange]
                      

                      这是非常有帮助的谈话。

                      【讨论】:

                        【解决方案18】:

                        Swift 4、iOS 12 和 Xcode 10 更新

                        viewDidLoad()里面放一行

                        navigationController?.navigationBar.barTintColor = UIColor.red
                        

                        【讨论】:

                          【解决方案19】:

                          Swift 5,一个带有 UINavigationController 扩展的简单方法。此答案的底部是 extensionspreviews

                          第一个视图控制器(主页):

                          override func viewWillAppear(_ animated: Bool) {
                              super.viewWillAppear(animated)
                          
                              navigationController?.setTintColor(.white)
                              navigationController?.backgroundColor(.orange)
                          }
                          

                          第二个视图控制器(详情):

                          override func viewWillAppear(_ animated: Bool) {
                              super.viewWillAppear(animated)
                              navigationController?.transparentNavigationBar()
                              navigationController?.setTintColor(.black)
                          }
                          

                          UINavigationController 的扩展:

                          extension UINavigationController {
                              func transparentNavigationBar() {
                                  self.navigationBar.setBackgroundImage(UIImage(), for: .default)
                                  self.navigationBar.shadowImage = UIImage()
                                  self.navigationBar.isTranslucent = true
                              }
                          
                              func setTintColor(_ color: UIColor) {
                                  self.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: color]
                                  self.navigationBar.tintColor = color
                              }
                          
                              func backgroundColor(_ color: UIColor) {
                                  navigationBar.setBackgroundImage(nil, for: .default)
                                  navigationBar.barTintColor = color
                                  navigationBar.shadowImage = UIImage()
                              }
                          }
                          

                          故事板视图:

                          预览:

                          【讨论】:

                            【解决方案20】:

                            在 Swift 2 中

                            用于改变导航栏的颜色,

                            navigationController?.navigationBar.barTintColor = UIColor.whiteColor()
                            

                            用于更改项目导航栏中的颜色,

                            navigationController?.navigationBar.tintColor = UIColor.blueColor()
                            

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

                            【讨论】:

                              【解决方案21】:

                              斯威夫特 3

                              UINavigationBar.appearance().barTintColor = UIColor(colorLiteralRed: 51/255, green: 90/255, blue: 149/255, alpha: 1)
                              

                              这会将您的导航栏颜色设置为 Facebook 栏颜色:)

                              【讨论】:

                                【解决方案22】:

                                Swift 3 和 Swift 4 兼容 Xcode 9

                                为常见导航栏创建一个类的更好解决方案

                                我有 5 个控制器,每个控制器的标题都更改为橙色。由于每个控制器都有 5 个导航控制器,所以我不得不从检查器或代码中更改每一种颜色。

                                所以我创建了一个类,而不是从代码中更改每个导航栏,我只是分配了这个类,它适用于所有 5 个控制器代码重用能力。 您只需将此类分配给每个控制器即可。

                                import UIKit
                                
                                   class NabigationBar: UINavigationBar {
                                      required init?(coder aDecoder: NSCoder) {
                                       super.init(coder: aDecoder)
                                    commonFeatures()
                                 }
                                
                                   func commonFeatures() {
                                
                                    self.backgroundColor = UIColor.white;
                                      UINavigationBar.appearance().titleTextAttributes =     [NSAttributedStringKey.foregroundColor:ColorConstants.orangeTextColor]
                                
                                 }
                                
                                
                                  }
                                

                                【讨论】:

                                  【解决方案23】:

                                  iOS 10 Swift 3.0

                                  如果您不介意使用 swift 框架,那么我们 UINeraida 将导航背景更改为 UIColorHexColorUIImage 并以编程方式更改导航返回按钮文本,更改完整的前景文本颜色。

                                  UINavigationBar

                                      neraida.navigation.background.color.hexColor("54ad00", isTranslucent: false, viewController: self)
                                      
                                      //Change navigation title, backbutton colour
                                      
                                      neraida.navigation.foreground.color.uiColor(UIColor.white, viewController: self)
                                      
                                      //Change navigation back button title programmatically
                                      
                                      neraida.navigation.foreground.backButtonTitle("Custom Title", ViewController: self)
                                      
                                      //Apply Background Image to the UINavigationBar
                                      
                                      neraida.navigation.background.image("background", edge: (0,0,0,0), barMetrics: .default, isTranslucent: false, viewController: self)
                                  

                                  【讨论】:

                                    【解决方案24】:

                                    斯威夫特 3

                                    可以在ViewDidLoad()中使用的简单的一种衬里

                                    //Change Color
                                        self.navigationController?.navigationBar.barTintColor = UIColor.red
                                    //Change Text Color
                                        self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
                                    

                                    【讨论】:

                                      【解决方案25】:

                                      我不得不这样做

                                      UINavigationBar.appearance().tintColor = UIColor.whiteColor()
                                      UINavigationBar.appearance().barStyle = .Black
                                      UINavigationBar.appearance().backgroundColor = UIColor.blueColor()
                                      

                                      否则背景颜色不会改变

                                      【讨论】:

                                        【解决方案26】:

                                        首先将navigationBar 的isTranslucent 属性设置为false 以获得想要的颜色。然后像这样更改导航栏颜色:

                                        @IBOutlet var NavigationBar: UINavigationBar!
                                        
                                        NavigationBar.isTranslucent = false
                                        NavigationBar.barTintColor = UIColor (red: 117/255, green: 23/255, blue: 49/255, alpha: 1.0)
                                        

                                        【讨论】:

                                          【解决方案27】:

                                          确保为 .normal 设置 Button State

                                          extension UINavigationBar {
                                          
                                              func makeContent(color: UIColor) {
                                                  let attributes: [NSAttributedString.Key: Any]? = [.foregroundColor: color]
                                          
                                                  self.titleTextAttributes = attributes
                                                  self.topItem?.leftBarButtonItem?.setTitleTextAttributes(attributes, for: .normal)
                                                  self.topItem?.rightBarButtonItem?.setTitleTextAttributes(attributes, for: .normal)
                                              }
                                          }
                                          

                                          PS iOS 12,Xcode 10.1

                                          【讨论】:

                                          • 谢谢。我一直在寻找这个topItem 解决方案的时间。令人沮丧的是,Apple 继续对如何将样式应用于导航进行更改。
                                          【解决方案28】:

                                          在 AppDelegate 中试试这个:

                                          //MARK:- ~~~~~~~~~~setupApplicationUIAppearance Method
                                          func setupApplicationUIAppearance() {
                                          
                                              UIApplication.shared.statusBarView?.backgroundColor = UIColor.clear
                                          
                                              var preferredStatusBarStyle: UIStatusBarStyle {
                                                  return .lightContent
                                              }
                                          
                                              UINavigationBar.appearance().tintColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
                                              UINavigationBar.appearance().barTintColor =  UIColor.white
                                              UINavigationBar.appearance().isTranslucent = false
                                          
                                              let attributes: [NSAttributedString.Key: AnyObject]
                                          
                                              if DeviceType.IS_IPAD{
                                                  attributes = [
                                                      NSAttributedString.Key.foregroundColor: UIColor.white,
                                                      NSAttributedString.Key.font: UIFont(name: "HelveticaNeue", size: 30)
                                                      ] as [NSAttributedString.Key : AnyObject]
                                              }else{
                                                  attributes = [
                                                      NSAttributedString.Key.foregroundColor: UIColor.white
                                                  ]
                                              }
                                              UINavigationBar.appearance().titleTextAttributes = attributes
                                          }
                                          

                                          iOS 13

                                          func setupNavigationBar() {
                                              //        if #available(iOS 13, *) {
                                              //            let window = UIApplication.shared.windows.filter {$0.isKeyWindow}.first
                                              //            let statusBar = UIView(frame: window?.windowScene?.statusBarManager?.statusBarFrame ?? CGRect.zero)
                                              //            statusBar.backgroundColor = #colorLiteral(red: 0.2784313725, green: 0.4549019608, blue: 0.5921568627, alpha: 1) //UIColor.init(hexString: "#002856")
                                              //            //statusBar.tintColor = UIColor.init(hexString: "#002856")
                                              //            window?.addSubview(statusBar)
                                              //            UINavigationBar.appearance().tintColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
                                              //            UINavigationBar.appearance().barTintColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
                                              //            UINavigationBar.appearance().isTranslucent = false
                                              //            UINavigationBar.appearance().backgroundColor = #colorLiteral(red: 0.2784313725, green: 0.4549019608, blue: 0.5921568627, alpha: 1)
                                              //            UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor.white]
                                              //        }
                                              //        else
                                              //        {
                                              UIApplication.shared.statusBarView?.backgroundColor = #colorLiteral(red: 0.2784313725, green: 0.4549019608, blue: 0.5921568627, alpha: 1)
                                              UINavigationBar.appearance().tintColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
                                              UINavigationBar.appearance().barTintColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
                                              UINavigationBar.appearance().isTranslucent = false
                                              UINavigationBar.appearance().backgroundColor = #colorLiteral(red: 0.2784313725, green: 0.4549019608, blue: 0.5921568627, alpha: 1)
                                              UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor.white]
                                              //        }
                                          }
                                          

                                          扩展

                                          extension UIApplication {
                                          
                                          var statusBarView: UIView? {
                                              if responds(to: Selector(("statusBar"))) {
                                                  return value(forKey: "statusBar") as? UIView
                                              }
                                              return nil
                                          }}
                                          

                                          【讨论】:

                                            【解决方案29】:

                                            我正在为那些仍然对这里的解决方案有问题的人写这篇文章。

                                            我使用的是 Xcode 版本 11.4 (11E146)。为我工作的是:

                                            navigationController?.navigationBar.barTintColor = UIColor.white
                                            navigationController?.navigationBar.tintColor = UIColor.black
                                            

                                            但是!如果您将情节提要中的 barTintColor 设置为“默认”以外的任何其他值,则这 2 行代码将无效。

                                            因此,请小心并在 Storyboard 中设置回默认 barTintColor。 哦,苹果……

                                            【讨论】:

                                            • 尽管将色调更改为默认颜色,但仍然存在同样的问题:(
                                            • @marika.daboja 故事板中的所有导航控制器都设置为默认颜色?
                                            • 嗨,我只有 1 个导航控制器(外加 2 个表视图控制器)。导航控制器栏色调颜色设置为“默认”。我必须更新此颜色的代码似乎对它没有影响。
                                            • @marika.daboja 确保您的 navigationController 不为零。并且你把这行代码放在 viewDidLoad()
                                            【解决方案30】:

                                            如果您使用的是 iOS 13 或 14 和大标题,并且想要更改导航栏颜色,请使用以下代码:

                                            参考barTintColor not applied when NavigationBar is Large Titles

                                                fileprivate func setNavigtionBarItems() {
                                                    if #available(iOS 13.0, *) {
                                                        let appearance = UINavigationBarAppearance()
                                                        appearance.configureWithDefaultBackground()
                                                        appearance.backgroundColor = .brown
                                            //            let naviFont = UIFont(name: "Chalkduster", size: 30) ?? .systemFont(ofSize: 30)
                                            //            appearance.titleTextAttributes = [NSAttributedString.Key.font: naviFont]
                                                        
                                                        navigationController?.navigationBar.prefersLargeTitles = true
                                                        navigationController?.navigationBar.standardAppearance = appearance
                                                        navigationController?.navigationBar.scrollEdgeAppearance = appearance
                                                        //navigationController?.navigationBar.compactAppearance = appearance
                                                    } else {
                                                        // Fallback on earlier versions
                                                        navigationController?.navigationBar.barTintColor = .brown
                                                    }
                                                }
                                            

                                            这花了我 1 个小时才弄清楚我的代码有什么问题:(,因为我使用的是大标题,所以很难用 largeTitle 更改 tintColor,为什么苹果让它如此复杂,这么多行只是制作navigationBar的tintColor。

                                            【讨论】:

                                              猜你喜欢
                                              • 2015-11-03
                                              • 2021-01-01
                                              • 2016-06-28
                                              • 2018-02-06
                                              • 1970-01-01
                                              • 1970-01-01
                                              • 1970-01-01
                                              相关资源
                                              最近更新 更多