更改UIBarButtonItem的色调颜色

And*_*nez 44 objective-c uibarbuttonitem tintcolor ios ios7

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

在此输入图像描述

这让我疯了.因为这个对象是动态创建的,所以我无法在IB中设置它的颜色(就像我以前的bar按钮项一样).

我尝试过的解决方案包括:

  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"设置为白色,它始终是默认的蓝色.

hgw*_*tle 83

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

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

Apple iOS 7 UI过渡指南中的更详细信息(特别是在"使用色调颜色"部分下).

***要么***

根据一些注释,您还可以使用UINavigationBar外观代理实现此目的.这将仅影响UIBarButtonItems的tintColor,而不是在窗口上设置tintColor并影响该窗口的所有子视图.

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

    return YES;
}
Run Code Online (Sandbox Code Playgroud)

  • 我只能假设你的rootViewController逻辑中的某个地方发生了一些奇怪的事情,这会影响tintColor.我会看一下Apple关于tintColor继承如何在封面下工作的文档. (2认同)

小智 23

我认为您正在寻找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];`不设置`UIBarButtonItem的颜色 (13认同)
  • 这不适合我.尝试了每一个组合 (3认同)
  • 不,它确实设置了它 - 查看上面的链接,“导航栏的外观”部分。实际上,这是唯一正确的实施方式。要更改条形本身的色调,您应该使用 barTintColor。 (2认同)

Ash*_*k R 10

在Swift 3.0中

let navigationBarAppearnce = UINavigationBar.appearance()
Run Code Online (Sandbox Code Playgroud)

导航栏的tintColor会影响后指示图像,按钮标题和按钮图像的颜色.

navigationBarAppearnce.barTintColor = UIColor(red: 0.180, green: 0.459, blue: 0.733, alpha: 1.00)
Run Code Online (Sandbox Code Playgroud)

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

navigationBarAppearnce.tintColor = UIColor.white
Run Code Online (Sandbox Code Playgroud)

最终守则

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
}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


Mit*_*gam 5

斯威夫特 5

barButtonItem.setTitleTextAttributes([NSAttributedString.Key.foregroundColor : UIColor.white], for: .normal)
Run Code Online (Sandbox Code Playgroud)