【问题标题】:Changing the Tint Color of UIBarButtonItem更改 UIBarButtonItem 的 Tint 颜色
【发布时间】:2013-10-30 12:31:32
【问题描述】:

我有一个使用 Storyboard 的项目,每当我使用 segue 推送视图控制器时,动态创建的条形按钮项总是蓝色的。

这让我发疯了。因为这个对象是动态创建的,所以我无法在 IB 中设置它的颜色(就像我对之前的条形按钮项目所做的那样)。

我尝试过的解决方案有:

  1. 设置在接收者的viewDidLoad
  2. 设置在接收者的viewDidAppear

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

  3. 当我发现这不太行时,我尝试设置 leftBarButtonItem:

self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];

  1. 在调用新视图时以及在推送新视图之前,我在应用程序的委托中尝试了以下代码(我从其他 SO 答案中获得):

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

我发现的所有谷歌答案都建议使用上面的代码,但它对我来说根本不起作用。或许 iOS 7 的外观 API 有一些变化?无论我如何或在何处尝试将“Categorías”设置为白色,它始终是默认的蓝色。

【问题讨论】:

    标签: ios objective-c ios7 uibarbuttonitem tintcolor


    【解决方案1】:

    在 iOS 7 中,要设置应用程序中所有 barButtonItems 的颜色,请在 AppDelegate 中的应用程序窗口上设置 tintColor 属性。

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        self.window.tintColor = [UIColor whiteColor];
        return YES;
    }
    

    Apple's iOS 7 UI Transition Guide 中的更多详细信息(特别是在“使用色调颜色”部分下)。

    ***或***

    基于一些cmets,你也可以通过UINavigationBar外观代理来实现。这将只影响 UIBarButtonItems 的 tintColor,而不是在窗口上设置 tintColor 并影响该窗口的所有子视图。

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        if([UINavigationBar conformsToProtocol:@protocol(UIAppearanceContainer)]) {
            [UINavigationBar appearance].tintColor = [UIColor whiteColor];
        }
    
        return YES;
    }
    

    【讨论】:

    • 白色以外的任何颜色都无法正常工作
    • 定义“不按预期工作”。我将self.window.tintColor 设置为的任何颜色都将成为我的 barButtonItems 的颜色。正如预期的那样。
    • 我根据注销和登录更改了窗口的rootViewController。当我将 self.window.tintColor 设置为 whiteColor 以外的任何颜色时,它仅第一次起作用。然后,当我将 window.rootViewController 更改为其他内容时,颜色会变暗(未禁用,),并且当 whiteColor 设置为 tintColor 时不会发生这种情况
    • 我只能假设在您的 rootViewController 逻辑中某处发生了一些奇怪的事情,这会影响 tintColor。我会查看 Apple 的文档,了解 tintColor 继承如何在幕后工作。
    • 我解决了这个问题...当我更改窗口的 rootViewController 时仍然存在弹出窗口...现在在将 window.rootViewController 设置为新的视图控制器之前,我关闭了 popovercontroller
    【解决方案2】:

    我认为您正在寻找 UINavigationBar 的属性。尝试设置self.navigationController.navigationBar.tintColor = [UIColor whiteColor];

    请参阅“导航栏的外观”部分:https://developer.apple.com/library/ios/documentation/userexperience/conceptual/UIKitUICatalog/UINavigationBar.html#//apple_ref/doc/uid/TP40012857-UINavigationBar-SW1

    【讨论】:

    • self.navigationController.navigationBar.tintColor = [UIColor whiteColor]; 没有设置UIBarButtonItems的颜色
    • 不,它确实设置了它 - 查看上面的链接,“导航栏的外观”部分。实际上,这是唯一正确的实施方式。要更改条本身的色调,您应该使用 barTintColor。
    • Argh...不知怎的,我遇到了UINavigationController.navigationBar.tintColor = ... 不起作用的情况...我在这种情况下的工作是UIViewController *currentViewController = self.childViewControllers.lastObject; [currentViewController.navigationItem.leftBarButtonItems enumerateObjectsUsingBlock:^(UIBarButtonItem * _Nonnull barButtonItem, NSUInteger idx, BOOL * _Nonnull stop) { barButtonItem.tintColor = YOUR_COLOR; }]; // REPEAT SAME THINGS FOR RIGHT BAR BUTTON ITEMS AND BACK BUTTON ITEM 绝对不理想,但它可能会有所帮助
    【解决方案3】:

    在 Swift 3.0 中

    let navigationBarAppearnce = UINavigationBar.appearance()
    

    导航栏的tintColor 会影响后退指示符图像、按钮标题和按钮图像的颜色。

    navigationBarAppearnce.barTintColor = UIColor(red: 0.180, green: 0.459, blue: 0.733, alpha: 1.00)
    

    barTintColor 属性会影响条形本身的颜色

    navigationBarAppearnce.tintColor = UIColor.white
    

    最终代码

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    
     let navigationBarAppearnce = UINavigationBar.appearance()
    
     navigationBarAppearnce.barTintColor = UIColor(red: 0.180, green: 0.459, blue: 0.733, alpha: 1.00)
    
     navigationBarAppearnce.tintColor = UIColor.white
    
     navigationBarAppearnce.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white]
    
     //Change status bar color
     UIApplication.shared.statusBarStyle = .lightContent
    
     return true
    }
    

    【讨论】:

      【解决方案4】:

      斯威夫特 5

      barButtonItem.setTitleTextAttributes([NSAttributedString.Key.foregroundColor : UIColor.white], for: .normal)
      

      【讨论】:

        【解决方案5】:

        要更改导航栏中特定项目(例如按钮)的颜色: 在 Objective-C 中

        myButton.tintColor = [UIColor redColor];
        

        【讨论】:

          【解决方案6】:

          在 iOS 8 中,如果您出于某种目的更改 UIView 色调颜色,例如为 UIAlertView 品牌化,则 UIToolBar 中 UIBarButtonItem 的色调颜色也会以这种方式更改。要解决此问题,只需编写此代码

          [UIView appearance].tintColor = SOME_COLOR;
          [UIView appearanceWhenContainedIn:[UIToolbar class], nil].tintColor = BLACK_COLOR;
          

          对于 UINavigationBar 中的 UIBarButtonItem 着色使用标准方法

          [UINavigationBar appearance].tintColor = BLACK_COLOR;
          

          【讨论】:

          • 这个答案突出了一个非常重要的点。 UIView.appearance() 覆盖 UINavigationBar.appearance()
          • [UIView 外观].tintColor 为我解决了我的问题 +1
          【解决方案7】:
          UITabBar.appearance().tintColor = UIColor.yellowColor()
          

          【讨论】:

          • 错误的酒吧同胞。这个询问导航栏。然而,解决方案几乎是正确的。
          【解决方案8】:

          这对我有用

          AddBarButtonItem.setTitleTextAttributes([NSAttributedString.Key.foregroundColor : UIColor.white], for: .normal)
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2020-04-22
            • 1970-01-01
            • 1970-01-01
            • 2015-11-11
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多