通过整个应用程序更改导航项颜色?

时间:2013-10-25 19:00:10

标签: ios objective-c

我正在尝试更改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]};

6 个答案:

答案 0 :(得分:30)

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

您想要更改UIBarButtonItem色调颜色。

您可以使用UIAppearance代理

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

这将更改应用程序中每个tintColor的{​​{1}}。

您可以使用相同的策略更改UIBarButtonItem UINavigationBarbarTintColor

titleTextAttributes

不幸的是there's no way of changing the translucent property using the proxy,所以你必须在每个栏上都这样做。

答案 1 :(得分:3)

快速回答

A1

将它放在AppDelegate的func应用程序中(应用程序:UIApplication,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()

答案 2 :(得分:0)

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

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

另请查看我在这里推荐阅读和查看材料的答案: StatusBar bug in iOS7?

答案 3 :(得分:0)

superViewDidLoad方法中。

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

它会改变导航项目颜色的颜色。 按钮也是如此。

答案 4 :(得分:0)

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

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

使用iOS 9.3,iPhone和iPad进行测试。您的navigationBar标题仍将使用默认颜色进行着色。这有点令人困惑。

答案 5 :(得分:0)

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

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