【问题标题】:Change color of Back button in navigation bar更改导航栏中后退按钮的颜色
【发布时间】:2015-04-28 07:51:21
【问题描述】:

我正在尝试将“设置”按钮的颜色更改为白色,但无法更改。

这两个我都试过了:

navigationItem.leftBarButtonItem?.tintColor = UIColor.whiteColor()
navigationItem.backBarButtonItem?.tintColor = UIColor.whiteColor()

但没有变化,它仍然是这样的:

如何使那个按钮变白?

【问题讨论】:

标签: ios swift uinavigationbar uibarbuttonitem backbarbuttonitem


【解决方案1】:

您可以通过单击板上的空白区域并在右侧工具栏中选择“显示文件检查器”来更改情节提要中的全局色调颜色,您将在工具栏底部看到“全局色调”选项。

【讨论】:

  • 哇.. 很好的发现!您知道如何以编程方式执行此操作吗?
  • 很棒的发现,但将全局色调更改为白色(根据原始问题)也会将表格视图详细信息披露指示符更改为白色,这会使它们在相关表格中不可见。
  • 补充 Mike 的问题:这个控件还让你所有的文本按钮都变成这个颜色,所以你要么必须手动将它们设置为一种颜色(即显然没有默认颜色),要么只使用不同的方法。
  • 虽然这是一种快速简便的修复方法,但如果颜色为白色/与背景匹配,则会导致其他问题。如果您有文本字段,它会使光标不明显,这使用户看起来好像他们无法在文本字段上书写,因为光标不可见。
  • @PaulLehn 要以编程方式执行此操作,您可以在 AppDelegate.swift > application(application:didFinishLaunchingWithOptions:) 中编写:self.window!.tintColor = .yourColor
【解决方案2】:

这段代码改变了箭头颜色

self.navigationController.navigationBar.tintColor = UIColor.whiteColor();

如果这不起作用,请使用以下代码:

self.navigationBar.barStyle = UIBarStyle.Black
self.navigationBar.tintColor = UIColor.whiteColor()

Swift 3 笔记

UIColor.whiteColor() 和类似的已被简化为UIColor.white

此外,许多以前的隐式选项已更改为显式,因此您可能需要:

self.navigationController?.navigationBar =

【讨论】:

  • self.navigationController?.navigationBar.tintColor = UIColor.whiteColor() 为我工作(Swift 2.2)
  • self.navigationBar.barStyle = UIBarStyle.Black self.navigationBar.tintColor = UIColor.whiteColor() 为我工作(Swift 3)
  • 你能解释一下为什么要使用 UIBarStyle.Black 吗?
  • 请更新:self.navigationController?.navigationBar.tintColor =
  • 它只改变了箭头,Xcode 11 中的返回文本仍然保持不变
【解决方案3】:

在情节提要中很容易设置:

【讨论】:

    【解决方案4】:

    你应该使用这个:

    navigationController?.navigationBar.barTintColor = .purple
    navigationController?.navigationBar.tintColor = .white
    

    【讨论】:

    • 这使按钮变白,但背景颜色变浅了。你知道如何让酒吧的背景再次变成全彩吗?
    • 这是正确答案。通过将 barTintColor 和标题/栏按钮设置为白色,它将使栏变成紫色。
    【解决方案5】:

    斯威夫特

     override func viewDidLoad() {
         super.viewDidLoad()
    
     self.navigationController?.navigationBar.tintColor = UIColor.white
     }
    

    【讨论】:

    • 在 swift 3 中使用。
    • 也告诉我你可以使用哪个 swift 版本?
    • 在项目中哪个类/文件/vc中写了这段代码?
    • 在 viewWillAppear 上调用的函数 setCloseButton() 的开头。
    • 写入NextViewController.swift文件,也写入viewDidLoad()。
    【解决方案6】:

    斯威夫特 5.5

    更改完整的应用主题

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
         // Set for app
         UINavigationBar.appearance().tintColor = .white
         return true
    }
    

    更改特定控制器

    let navController = UINavigationController.init(rootViewController: yourViewController) 
    navController.navigationBar.tintColor = .red
    
    present(navController, animated: true, completion: nil)
    

    【讨论】:

    • 它改变了所有应用程序的颜色。如果我需要为特定屏幕/特定 UINavigationController 更改它怎么办?
    • @VyachaslavGerchicov 以上答案适用于完整的应用程序主题,如果您想更改特定的控制器。做这样的事情:让 navController = UINavigationController.init(rootViewController: yourViewController) navController.navigationBar.tintColor = .red
    • 如果您在 iOS 13 上不再使用 didFinishLaunchingWithOptions,它已移至 SceneDelegate。
    【解决方案7】:

    你可以像这样使用。把它放在AppDelegate.swift里面。

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
            // Override point for customization after application launch.
    
            UINavigationBar.appearance().translucent = false
            UINavigationBar.appearance().barTintColor = UIColor(rgba: "#2c8eb5")
            UINavigationBar.appearance().tintColor = UIColor.whiteColor()
            UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName:UIColor.whiteColor()]
    
            return true
        }
    

    【讨论】:

      【解决方案8】:

      Swift3中,将返回按钮设置为red

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

      【讨论】:

      • 这将设置导航栏,而不是栏按钮项
      • 是的,就是这个问题。
      • "改变导航栏中返回按钮的颜色"
      • 这是唯一对我有用的方法。 Apple 现在的实施似乎有些问题......
      【解决方案9】:

      在 Swift 4 中,您可以使用以下方法解决此问题:

      let navStyles = UINavigationBar.appearance()
      // This will set the color of the text for the back buttons.
      navStyles.tintColor = .white
      // This will set the background color for navBar
      navStyles.barTintColor = .black
      

      【讨论】:

        【解决方案10】:
            self.navigationController?.navigationBar.tintColor = UIColor.black // to change the all text color in navigation bar or navigation 
            self.navigationController?.navigationBar.barTintColor = UIColor.white // change the navigation background color
            self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.black] // To change only navigation bar title text color
        

        【讨论】:

        • 适用于 Swift 5
        【解决方案11】:

        斯威夫特 3

        对于 Swift 3,最受好评的答案不正确。

        改变颜色的正确代码是:

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

        如果要更改颜色,请将上面的 UIColor.white 更改为所需的颜色

        【讨论】:

        • 这是唯一对我有用的方法。 Apple 现在的实施似乎有些问题......
        【解决方案12】:

        所有答案设置UINavigationBar.appearance().tintColorUIAppearance.h 中的Apple 文档冲突。

        iOS7 的注意事项:在 iOS7 上,tintColor 属性已移至 UIView,现在具有在 UIView.h 中描述的特殊继承行为。 这种继承的行为可能与外观代理冲突,因此tintColor 现在不允许使用外观代理。

        在 Xcode 中,您需要通过命令单击要与外观代理一起使用的每个属性来检查头文件并确保该属性带有 UI_APPEARANCE_SELECTOR 注释。

        因此,通过外观代理将整个应用程序中的导航栏设置为紫色并将标题和按钮设置为白色的正确方法是:

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

        注意UIBarButtonItem 不是UIView 的子类,而是NSObject。所以它的tintColor 属性不是从UIView 继承的tintColor

        不幸的是,UIBarButtonItem.tintColor 没有用UI_APPEARANCE_SELECTOR 注释——但这在我看来是一个文档错误。 Apple Engineering 在this radar 中的回复表明它是受支持的。

        【讨论】:

        • UINavigationBar.appearance().largeTitleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white] //也是
        【解决方案13】:

        AppDelegate 类中使用此代码,在didFinishLaunchingWithOptions 内部。

        func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        
        UINavigationBar.appearance().tintColor = .white
        
        }
        

        【讨论】:

          【解决方案14】:
          self.navigationController?.navigationBar.tintColor = UIColor.redColor()
          

          这个 sn-p 具有魔力。而不是 redColor,根据您的意愿将其更改为。

          【讨论】:

            【解决方案15】:

            让我们试试这段代码:

             func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
                // Override point for customization after application launch.
            
                let navigationBarAppearace = UINavigationBar.appearance()
                navigationBarAppearace.tintColor = UIColor.whiteColor()  // Back buttons and such
                navigationBarAppearace.barTintColor = UIColor.purpleColor()  // Bar's background color
                navigationBarAppearace.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.whiteColor()]  // Title's text color
            
                self.window?.backgroundColor = UIColor.whiteColor()
                return true
            }
            

            【讨论】:

            • 非常感谢您,先生
            【解决方案16】:

            在 swift 2.0 中使用

            self.navigationController!.navigationBar.tintColor = UIColor.whiteColor();
            

            【讨论】:

              【解决方案17】:

              如果您的“设置”视图控制器中已经有后退按钮,并且您想将“付款信息”视图控制器上的后退按钮颜色更改为其他颜色,您可以在“设置”视图控制器的准备中执行此操作像这样继续:

              override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
                  if segue.identifier == "YourPaymentInformationSegue"
                  {
                      //Make the back button for "Payment Information" gray:
                      self.navigationItem.backBarButtonItem?.tintColor = UIColor.gray               
                  }
              }
              

              【讨论】:

                【解决方案18】:

                如果你试了很多次还是不行,你可以试试:

                UIBarButtonItem.appearance(whenContainedInInstancesOf: [UINavigationBar.self]).tintColor = .red
                

                其实我试过很多次,才发现这种方法行得通。

                【讨论】:

                • 你是一个生活品味,没有其他解决方案对我有用,谢谢队友
                【解决方案19】:
                self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
                

                这对我有用,iOS 9.0+

                【讨论】:

                  【解决方案20】:

                  不知道为什么没有人提到这一点......但我正在做你在我的 viewDidLoad 中所做的事情......但它不起作用。然后我将代码放入 viewWillAppear 中,一切正常。

                  上面的解决方法是换一个单个 barbuttonItem。如果您想更改代码中每个导航栏的颜色,请关注this answer

                  基本上使用appearance() 更改类本身就像在应用程序中对该视图的所有实例进行全局更改。更多内容见here

                  【讨论】:

                    【解决方案21】:

                    AppDelegate.swift

                    中的 didFinishLaunchingWithOptions 函数中添加以下代码
                    var navigationBarAppearace = UINavigationBar.appearance()
                    
                    navigationBarAppearace.tintColor = uicolorFromHex(0xffffff) // White color
                    navigationBarAppearace.barTintColor = uicolorFromHex(0x034517) // Green shade
                    
                    // change navigation item title color
                    navigationBarAppearace.titleTextAttributes =[NSForegroundColorAttributeName:UIColor.whiteColor()]
                    

                    【讨论】:

                    • 这段代码给了我以下错误:“使用未解析的标识符'uicolorFromHex'”谁能帮我解决这个问题?
                    • 可能是 UIColor 的扩展。你可以搜索stackoverflow如何创建扩展。
                    【解决方案22】:

                    对于 Swift 2.0,要更改 导航栏色调颜色标题文本后退按钮 色调颜色,请使用以下内容更改AppDelegate.swift

                    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
                    
                      // Override point for customization after application launch.
                    
                    
                        //Navigation bar tint color change
                    
                        UINavigationBar.appearance().barTintColor = UIColor(red: 42/255.0, green: 140/255.0, blue: 166/255.0, alpha: 0.5)
                    
                        //Back button tint color change
                    
                        UINavigationBar.appearance().barStyle = UIBarStyle.Default
                        UINavigationBar.appearance().tintColor =  UIColor(red: 204/255.0, green: 255/255.0, blue: 204/255.0, alpha: 1)
                    
                        //Navigation Menu font tint color change
                    
                        UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor(red: 204/255.0, green: 255/255.0, blue: 204/255.0, alpha: 1), NSFontAttributeName: UIFont(name: "OpenSans-Bold", size: 25)!]//UIColor(red: 42/255.0, green: 140/255.0, blue: 166/255.0, alpha: 1.0)
                    
                        UIApplication.sharedApplication().statusBarStyle = UIStatusBarStyle.LightContent
                    
                    
                        return true
                    }
                    

                    【讨论】:

                      【解决方案23】:

                      您有一个选择隐藏您的后退按钮并自己制作。 然后设置它的颜色。

                      我做到了:

                      self.navigationItem.setHidesBackButton(true, animated: true)
                      let backbtn = UIBarButtonItem(title: "Back", style:UIBarButtonItemStyle.Plain, target: self, action: "backTapped:")
                      self.navigationItem.leftBarButtonItem = backbtn
                      self.navigationItem.leftBarButtonItem?.tintColor = UIColor.grayColor()
                      

                      【讨论】:

                      • 这个问题专门针对左侧导航项的样式,这是最接近直接回答问题的方式。
                      【解决方案24】:

                      我更喜欢自定义 NavigationController 而不是设置全局 ui,或者放入 ViewController。

                      这是我的解决方案

                      
                      class AppNavigationController : UINavigationController {
                      
                        override func viewDidLoad() {
                          super.viewDidLoad()
                          self.delegate = self
                        }
                      
                        override func viewWillAppear(_ animated: Bool) {
                      
                        }
                      
                      }
                      extension AppNavigationController : UINavigationControllerDelegate {
                      
                        func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
                          let backButtonItem = UIBarButtonItem(
                            title: "   ",
                            style: UIBarButtonItem.Style.plain,
                            target: nil,
                            action: nil)
                          backButtonItem.tintColor = UIColor.gray
                          viewController.navigationItem.backBarButtonItem = backButtonItem
                        }
                      
                        func navigationController(_ navigationController: UINavigationController, didShow viewController: UIViewController, animated: Bool) {
                      
                        }
                      
                      }
                      
                      

                      如果你使用像UIBarButtonItem.appearance().tintColor = .white这样的全局设置用户界面,你也不需要像EKEventEditViewControllerPickerViewController等那样弄乱Apple Api。

                      【讨论】:

                        【解决方案25】:

                        我在 swift 5 中使用它并为我工作

                        navigationItem.backBarButtonItem?.tintColor = UIColor(named: "uberRed")
                        

                        【讨论】:

                          【解决方案26】:

                          用 -(void)viewDidLoad 中的这一行来解决:

                          self.navigationItem.backBarButtonItem.tintColor = UIColor.whiteColor;
                          

                          【讨论】:

                          • (lldb) p self.navigationItem.backBarButtonItem (UIBarButtonItem *) $9 = nil (lldb)
                          【解决方案27】:

                          你应该添加这一行

                           self.navigationController?.navigationBar.topItem?.backBarButtonItem?.tintColor = .black
                          

                          【讨论】:

                            【解决方案28】:

                            Swift 5.3:

                            UINavigationBar.appearance().backIndicatorImage = UIImage(named: "custom-back-image")
                            UINavigationBar.appearance().backIndicatorTransitionMaskImage = UIImage(named: "custom-back-image")
                            

                            【讨论】:

                              【解决方案29】:

                              Swift 5 更新

                              如果您需要全局设置Back 按钮颜色,您可以简单地使用:

                              UIBarButtonItem.appearance().tintColor = Asset.pureWhite.color
                              

                              那么你不需要在每个视图控制器上设置返回按钮背景颜色。如果你使用这个,你不能在另一个视图控制器上设置返回按钮颜色

                              但是

                              如果您需要在视图控制器上设置后退按钮颜色或在另一个视图控制器上更改,请不要使用上述方法。你可以使用:

                              let appearance = UINavigationBarAppearance()
                              appearance.titleTextAttributes = [.font:FontFamily.BatonTurbo.medium.font(size: 20),
                                                                .foregroundColor: Asset.pureWhite.color] // Naviagtion Title attributes
                              appearance.backgroundColor = .red // Navigation bar background color
                              
                              self.navigationItem.standardAppearance = appearance
                              self.navigationItem.scrollEdgeAppearance = appearance
                              self.navigationItem.compactAppearance = appearance
                              
                              navigationController?.navigationBar.tintColor = .green // Back button color
                              

                              【讨论】:

                                猜你喜欢
                                • 1970-01-01
                                • 1970-01-01
                                • 1970-01-01
                                • 1970-01-01
                                • 2011-12-17
                                • 2021-11-16
                                • 2015-07-01
                                • 1970-01-01
                                • 1970-01-01
                                相关资源
                                最近更新 更多