【问题标题】:setTintColor: on UINavigationBar appearance has no effectsetTintColor:对 UINavigationBar 外观没有影响
【发布时间】:2012-01-25 04:28:09
【问题描述】:

这是我更改应用标签栏和导航栏颜色的代码:

UIColor* color = [UIColor colorWithPatternImage:[UIImage imageNamed:@"blue.jpeg"]];

//set colors
[[UINavigationBar appearance] setTintColor:color];
[[UITabBar appearance] setTintColor:color];

然而只有标签栏的颜色会改变;导航栏保持黑色。为什么setTintColor: 对标签栏有效,而对导航栏无效?

编辑:有趣的是,在真实设备(运行 iOS 5.0.1 的 iPhone 4)上进行测试时,标签栏 导航栏的颜色都没有改变;两者都保持黑色。在模拟器上,至少标签栏发生了变化……对此行为有什么解释吗?谢谢!

编辑 2:这是我在 applicationDidFinishLaunching: 中的部分代码

UIColor* color = [UIColor colorWithPatternImage:[UIImage imageNamed:@"blue.jpeg"]];

//create navigation controllers
firstNavigationController = [[UINavigationController alloc] 
    initWithRootViewController:viewController1];
[[firstNavigationController navigationBar] setTintColor:color];
//[[firstNavigationController navigationBar] setBarStyle:UIBarStyleBlack];

secondNavigationController = [[UINavigationController alloc] 
    initWithRootViewController:viewController2];
[[secondNavigationController navigationBar] setTintColor:color];
//[[secondNavigationController navigationBar] setBarStyle:UIBarStyleBlack];

thirdNavigationController = [[UINavigationController alloc] 
    initWithRootViewController:viewController3];
[[thirdNavigationController navigationBar] setTintColor:color];
//[[thirdNavigationController navigationBar] setBarStyle:UIBarStyleBlack];

fourthNavigationController = [[UINavigationController alloc] 
    initWithRootViewController:viewController4];
[[fourthNavigationController navigationBar] setTintColor:color];
//[[fourthNavigationController navigationBar] setBarStyle:UIBarStyleBlack];

//create tab bar controller
self.tabBarController = [[UITabBarController new] autorelease];
self.tabBarController.delegate = self;

//set controllers
self.tabBarController.viewControllers = [NSArray 
    arrayWithObjects:firstNavigationController, secondNavigationController, 
    thirdNavigationController, fourthNavigationController, nil];

【问题讨论】:

  • 在这里快速尝试一下。专门引用一个 UINavigationBar 并更改它的 tintColor 属性,而不是去全局实现看看是否可行。

标签: ios uinavigationbar uitabbar uicolor appearance


【解决方案1】:

从“blue.jpeg”中找到 RGB 并将它们设置为 tint color...

self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:213.0/255.0 green:166.0/255.0 blue:39.0/255.0 alpha:1];

或者当您将图像设置为背景颜色时,您可以使用以下代码...

self.navigationController.navigationBar.layer.contents = (id)[UIImage imageNamed:@"blue.jpeg"].CGImage;

【讨论】:

    【解决方案2】:

    从更多的挖掘中,我了解到将 tintColor 属性设置为由图像制成的颜色被认为是“hack”。仅支持 RGB 颜色,并且预计该属性可以正常工作。所以你有它。由于保密协议,我无法提供更多细节,但一旦 iOS 6 公开,我会尝试更新。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-11
      • 2014-03-06
      • 1970-01-01
      • 2017-07-05
      • 1970-01-01
      • 1970-01-01
      • 2020-11-10
      • 2015-11-04
      相关资源
      最近更新 更多