13

我正在尝试更改 iOS 7 中后退按钮的色调颜色。现在有没有办法将整个应用程序中的所有导航项更改为特定颜色?这是我现在在一个视图控制器中所拥有的:

self.editButtonItem.tintColor = [UIColor whiteColor];
    self.navigationItem.backBarButtonItem.tintColor = [UIColor whiteColor];
    self.navigationController.navigationBarHidden = NO;
    self.navigationController.navigationBar.barTintColor = [UIColor redColor];
    self.navigationController.navigationBar.translucent = NO;
    self.title = @"Inbox";
    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName : [UIColor whiteColor]};
4

6 回答 6

30

UINavigationItem不是视图,也没有颜色。

相反,您想要更改UIBarButtonItem色调颜色。

使用UIAppearance代理你可以做

[[UIBarButtonItem appearance] setTintColor:[UIColor whiteColor]];

这将改变应用程序tintColor中的每一个UIBarButtonItem

您可以使用相同的策略来更改UINavigationBar barTintColorand titleTextAttributes

[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]]; // this will change the back button tint
[[UINavigationBar appearance] setBarTintColor:[UIColor redColor]];
[[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];

不幸的是,无法translucent使用 proxy 更改属性,因此您必须在每个栏上都这样做。

于 2013-10-25T19:26:16.033 回答
3

快速回答

UINavigationBar.appearance().backgroundColor = UIColor.blackColor()
UINavigationBar.appearance().barTintColor = UIColor.blackColor()
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()]
UINavigationBar.appearance().tintColor = UIColor.blackColor()
UIBarButtonItem.appearance().tintColor = UIColor.blackColor()
UITabBar.appearance().backgroundColor = UIColor.blackColor()
UITabBar.appearance().tintColor = UIColor.blackColor()
UITabBar.appearance().unselectedItemTintColor = UIColor.darkGrayColor()

把它放在你的 AppDelegate 的 func 应用程序中(应用程序:UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

于 2017-02-24T11:19:38.253 回答
0

从 iOS7 开始,您可以为所有导航项设置 tintcolor,以保持整个应用程序的外观和感觉一致:

 if ([self.window respondsToSelector:@selector(setTintColor:)]) {
        self.window.tintColor = [UIColor redColor];
}

另请查看我的答案以获取推荐的阅读和查看材料: iOS7 中的状态栏错误?

于 2013-10-25T19:38:16.953 回答
0

superViewDidLoad方法中。

self.navigationController.navigationBar.barTintColor = [UIColor redColor];

它会改变导航项颜色的颜色。按钮也会发生同样的情况。

于 2014-06-13T08:58:42.950 回答
0

这适用于更改后退按钮颜色:

   [[UINavigationBar appearance] setTintColor:[UIColor redColor]];

在 iOS 9.3、iPhone 和 iPad 上测试。您的导航栏标题仍将使用默认颜色着色。这有点令人困惑。

于 2016-06-16T12:56:46.260 回答
0

对于标题标签颜色,我们可以使用

self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white]
于 2018-12-12T22:41:15.223 回答