8

我现在将 ios6 应用程序移植到 ios7 sdk(使用 Xcode 5 和小牛),我试图改变 UIBarButtonItem 颜色,这是我尝试做的:

self.navigationController.navigationBar.tintColor 

- 更改栏的颜色,但不更改项目的颜色

[[UINavigationBar appearance] setTintColor:[UIColor greenColor]];
[[UIBarButtonItem appearance] setTintColor:[UIColor greenColor]];

- 不工作,同样的错误颜色

self.navigationItem.rightBarButtonItem.tintColor = [UIColor greenColor];
self.navigationItem.leftBarButtonItem.tintColor = [UIColor greenColor];    

- 不工作,同样的错误颜色

    UIBarButtonItem *close = [[UIBarButtonItem alloc] 
                             initWithTitle:NSLocalizedString(@"Close",@"") 
                                     style:UIBarButtonItemStyleDone target:self
                                    action:@selector(closeAddressBook:)];

           close.tintColor = [UIColor greenColor];

- 不工作,同样的错误颜色

  for self.filterSegment.tintColor =  [UIColor greenColor] where UISegmentedControl *filterSegment;

我看到未选择的段颜色正确,但选定的段颜色相同。

有任何想法吗?

4

6 回答 6

15

感谢 WWDC 2013 - Customizing Your App's Appearance for iOS 7,弄清楚需要做什么。

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

这将过滤到您应用程序中的其他视图中,因此放置在初始屏幕上,如果您推到下一个屏幕,您将看到后退按钮也是红色的。

要更改导航栏颜色,请使用

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

如果您让您的应用程序在低于 iOS7 的设备上运行,您应该检查它是否响应选择器

if([self.navigationController.navigationBar respondsToSelector:@selector(barTintColor)]) {

}
于 2013-09-16T21:57:12.740 回答
10

对于 iOS7,当我希望更改个人的颜色时,此代码适用于我UIBarButtonItem

UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Title" style:UIBarButtonItemStyleBordered target:self action:nil];
[barButtonItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor redColor]} forState:UIControlStateNormal];
self.navigationItem.leftBarButtonItems = @[barButtonItem];
于 2013-09-25T16:22:01.693 回答
1

1-iOS 7中,该tintColor属性不再用于设置栏的颜色。相反,使用该barTintColor属性来更改背景颜色。您可以在 中插入以下didFinishLaunchingWithOptions:代码AppDelegate.m

[[UINavigationBar appearance] setBarTintColor:[UIColor yellowColor]];

2-在 iOS 7 中,所有条形按钮都是无边框的。后退按钮现在是 V 形加上前一个屏幕的标题(如果前一个屏幕的标题为 nil,则仅显示“后退”作为按钮标题)。要为后退按钮着色,您可以更改该tintColor属性,它提供了一种使用自定义颜色为您的应用程序皮肤的快速简单的方法。下面是一个示例代码片段:

[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
于 2013-10-06T01:09:07.960 回答
1

改为在您的应用程序实例上设置tintColor属性可能是个好主意。UIWindow如果您在整个应用程序中使用了标准的“强调”颜色,这将使用该颜色为应用程序中的每个控件着色。

于 2013-09-17T00:06:02.403 回答
0

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

这是设置后退按钮颜色的方法。

对于使用 IOS 6 开发应用程序的人来说,我们使用上面的代码来设置导航栏颜色,所以如果您没有删除以前的设置,您将不会看到返回按钮的更改。

于 2013-09-30T16:11:50.830 回答
-2

我终于想出了解决这个问题的方法。设置好按钮后,需要等待 5-7 毫秒(以 iPhone 5s 为例)。

UIBarButtonItem *button=[[UIBarButtonItem alloc]...

self.navigationItem.rightBarButtonItem=button;

[button performSelector:@selector(setTintColor:) withObject:[UIColor blueColor] afterDelay:0.1];

你不应该在设置之前设置颜色rightBarButtonItem。它应该适用于 iOS 6 和 7。

于 2013-10-25T02:50:46.487 回答