236

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

这两个我都试过了:

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

但没有变化,它仍然看起来像这样:

在此处输入图像描述

如何使那个按钮变白?

4

28 回答 28

316

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

故事板中的全局色调选项

于 2015-05-02T10:43:17.040 回答
237

此代码更改箭头颜色

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

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

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

斯威夫特 3 笔记

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

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

self.navigationController?.navigationBar =
于 2015-02-26T04:06:54.093 回答
104

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

在此处输入图像描述

在此处输入图像描述

于 2016-06-22T05:33:43.553 回答
66

你应该使用这个:

navigationController?.navigationBar.barTintColor = .purple
navigationController?.navigationBar.tintColor = .white
于 2015-02-26T04:01:34.837 回答
47

迅速

 override func viewDidLoad() {
     super.viewDidLoad()

 self.navigationController?.navigationBar.tintColor = UIColor.white
 }
于 2017-06-23T11:10:01.430 回答
25

斯威夫特 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)
于 2018-09-12T08:04:00.263 回答
21

你可以像这样使用。把它放在里面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
    }
于 2015-05-03T19:04:11.630 回答
15

Swift3中,将 Back 按钮设置为red.

self.navigationController?.navigationBar.tintColor = UIColor.red
于 2017-01-26T14:56:16.020 回答
13

在 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
于 2017-12-27T18:23:58.747 回答
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
于 2020-04-08T14:57:38.223 回答
8

所有答案设置都UINavigationBar.appearance().tintColor与 Apple 在UIAppearance.h.

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属性不是继承tintColorUIView

不幸的是,UIBarButtonItem.tintColor没有注释UI_APPEARANCE_SELECTOR- 但在我看来,这似乎是一个文档错误。Apple Engineering 在此雷达中的响应表明它是受支持的。

于 2018-03-23T16:20:22.613 回答
8

斯威夫特 3

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

在此处输入图像描述

改变颜色的正确代码是:

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

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

于 2017-02-17T22:00:12.847 回答
7

AppDelegate在类中使用此代码,在didFinishLaunchingWithOptions.

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

UINavigationBar.appearance().tintColor = .white

}
于 2017-04-20T09:31:51.507 回答
6
self.navigationController?.navigationBar.tintColor = UIColor.redColor()

这个片段有魔力。而不是 redColor,根据您的意愿将其更改为。

于 2015-02-26T05:55:01.100 回答
4

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

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               
    }
}
于 2017-07-07T09:41:56.147 回答
4

在 swift 2.0 中使用

self.navigationController!.navigationBar.tintColor = UIColor.whiteColor();
于 2016-05-07T19:58:51.193 回答
4

让我们试试这段代码:

 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
}
于 2016-04-08T05:38:43.833 回答
3

如果您尝试了很多次但无法正常工作,您可以尝试:

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

实际上,我尝试了很多次,才发现这种方法行得通。

于 2020-12-13T13:09:18.513 回答
3

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

上面的解决方案是更改单个barbuttonItem。如果您想更改代码中每个导航栏的颜色,请遵循此答案

基本上使用更改到类本身appearance()就像对应用程序中该视图的所有实例进行全局更改。更多请看这里

于 2017-05-16T00:11:34.697 回答
3
self.navigationController.navigationBar.tintColor = [UIColor whiteColor];

这对我有用,iOS 9.0+

于 2016-05-31T03:10:53.340 回答
2

在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()]
于 2015-02-26T05:53:07.993 回答
2

对于 Swift 2.0,要更改Navigation-bar tint colortitle textback button tint color 使用 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
}
于 2016-02-24T05:50:09.160 回答
2

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

我这样做了:

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()
于 2016-04-13T07:03:27.227 回答
1

我更喜欢自定义 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) {

  }

}

此外,如果您使用全局设置 ui ,您也不需要像EKEventEditViewControllerPickerViewController等那样弄乱 Apple ApiUIBarButtonItem.appearance().tintColor = .white

于 2019-06-05T13:00:18.607 回答
1

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

navigationItem.backBarButtonItem?.tintColor = UIColor(named: "uberRed")
于 2021-02-17T08:06:58.233 回答
0

你应该添加这一行

 self.navigationController?.navigationBar.topItem?.backBarButtonItem?.tintColor = .black
于 2019-03-27T07:47:23.110 回答
0

它将通过 -(void)viewDidLoad 中的这一行来解决:

self.navigationItem.backBarButtonItem.tintColor = UIColor.whiteColor;
于 2018-04-23T05:36:37.510 回答
0

斯威夫特 5.3:

UINavigationBar.appearance().backIndicatorImage = UIImage(named: "custom-back-image")
UINavigationBar.appearance().backIndicatorTransitionMaskImage = UIImage(named: "custom-back-image")
于 2020-09-24T18:16:26.510 回答