【问题标题】:Changing navigation items color through the entire app?通过整个应用程序更改导航项的颜色?
【发布时间】:2013-10-25 19:00:09
【问题描述】:

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

【问题讨论】:

标签: ios objective-c


【解决方案1】:

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

您想要更改 UIBarButtonItem 色调颜色。

使用UIAppearance 代理您可以做到

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

这将更改应用程序中每个UIBarButtonItemtintColor

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

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

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

【讨论】:

  • 太棒了,这基本上就是我要问的,但这非常有效。谢谢!
  • 不客气,请记住,这仅与 iOS 7 兼容。
  • 嗯.. 它改变了除了
  • 你是说后退按钮?尝试设置 tintColor 属性
  • 尝试更改导航栏的tintColor 属性。
【解决方案2】:

快速回答

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 {

【讨论】:

    【解决方案3】:

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

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

    还可以在此处查看我的答案以获取推荐的阅读和查看材料: StatusBar bug in iOS7?

    【讨论】:

    • 我认为用户问的是UINavigationBarUIBarButtonItem,你的回答比他问的更适用。
    【解决方案4】:

    superViewDidLoad 方法中。

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

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

    【讨论】:

    • 问题是关于整个应用程序的。
    【解决方案5】:

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

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

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

    【讨论】:

      【解决方案6】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-07-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多