iOS 7 UIBarButton 返回按钮箭头颜色

173

我正在尝试更改后退按钮箭头。

enter image description here

我目前使用以下代码来控制后退按钮的文本大小和颜色:

[[UIBarButtonItem appearance] setTitleTextAttributes:
  [NSDictionary dictionaryWithObjectsAndKeys:
    [UIColor whiteColor], UITextAttributeTextColor,
    [UIFont boldSystemFontOfSize:16.0f], UITextAttributeFont,
    [UIColor darkGrayColor], UITextAttributeTextShadowColor,
    [NSValue valueWithCGSize:CGSizeMake(0.0, -1.0)], UITextAttributeTextShadowOffset,
  nil] forState:UIControlStateNormal];

但如果我只想为返回按钮更改箭头的颜色,该怎么办?


1
你找到了改变返回按钮箭头颜色的解决方案吗? - OnkarK
1
@OMK 我最终改变了我的infolistproperty NavBarColor来使它工作,然后将实际的navbarcolor设置为不同的颜色。我不确定发生了什么,但这个解决方案对我起作用了。 - kevinl
1
UINavigationBar 的一些属性行为已经从 iOS 7 发生了改变。查看这个答案以了解其他属性的影响。 - Bhavin
请帮忙解决这个问题:http://stackoverflow.com/questions/29923813/ios-back-button-arrow-not-displayed - kresa
17个回答

438

更改特定导航控制器的后退按钮 Chevron 颜色:

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

*如果您正在使用带有多个导航控制器的应用程序,并且希望此chevron颜色适用于每个控制器,请使用外观代理来设置每个导航控制器的返回按钮chevron,如下所示:

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

为了更好地说明,在 Swift 中(感谢评论中的 Jay Mayu):

UINavigationBar.appearance().tintColor = UIColor.whiteColor()

3
如果您在整个应用程序中使用相同的导航控制器,这将仅适用于整个应用程序。我实际上建议使用Bart的建议。 - Kyle Clegg
1
我在答案中加入了有关多个导航控制器的注释。我仍然不同意Bart的建议是“正确”的答案,因为当您设置整个应用程序的色调颜色时会产生许多其他副作用。此外,原始问题并没有询问如何为整个应用程序设置主题,它只是询问如何为返回按钮设置主题。 - DiscDev
1
这似乎没有设置后向 chevron 的颜色。 - jcampbell1
2
@jcampbell1 - 我在我的几个应用程序中成功地使用了这个...也许你没有读到关于多个UINavigationControllers的警告。 - DiscDev
7
对于 Swift,UINavigationBar.appearance().tintColor = UIColor.whiteColor() 的含义是将导航栏的颜色设置为白色。 - Jay Mayu
显示剩余8条评论

57

你必须设置整个应用程序的 tintColor。

self.window.tintColor = [UIColor redColor];

或者在 Swift 3 中:

self.window?.tintColor = UIColor.blue

来源: iOS 7 UI 过渡指南


4
如果您只想设置返回按钮箭头的颜色(而不是整个应用程序),如原始问题所述,那么这个答案是错误的。请查看我在此处给出的答案以获得正确的答案:https://dev59.com/1GMl5IYBdhLWcg3wc2xp#18809412 - DiscDev
我在我的应用程序委托中使用了这个代码,将我的返回按钮更改为黑色。请将其放置在applicationDidFinishLaunchingWithOptions方法中... window.tintColor = [UIColor blackColor]; - Marc Watson
这在iOS 6上无法工作。有人知道该怎么办吗? - Gavjr
1
这导致了它影响了整个应用程序的tintColor,包括任何UIActivityViewController(用于共享)和MFMailComposeViewController(用于发送电子邮件)。这些对话框假定未修改tintColor...可能会导致一些丑陋的颜色交互。 - Mike Lambert
这会改变许多其他对象的色调,比如UITextField中的闪烁光标。绝对不推荐这样做。 - Hunter Monk

55
您可以使用该方法设置整个应用程序导航栏的颜色。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:
(NSDictionary *)launchOptions{
    [[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
}

23

可以只更改箭头的颜色(而不是返回按钮标题的颜色)的方法如下:

[[self.navigationController.navigationBar.subviews lastObject] setTintColor:[UIColor blackColor]];

导航栏包含 _UINavigationBarBackIndicatorView 类型的子视图(在子视图数组中的最后一项),它代表箭头。

结果是 带有不同颜色的返回按钮箭头和返回按钮标题的导航栏


5
依赖于系统视图中子视图的顺序是不明智的。 - Glenn Maynard
2
这个方法可行,而设置self.navigationController.navigationBar.tintColor却不起作用。 - Rich Fox
@GlennMaynard 没错 - 实现这个的最佳方式是通过迭代 self.navigationController.navigationBar.subviews 返回的子视图数组,直到找到返回按钮为止。 - Evan R
@EvanR 尽管 selmad 的解决方案对我有效,但迭代子视图数组却不行(我先尝试了这种方法,因为我同意你的看法)。如果这种方法适用于您,请添加一个示例。谢谢。 - jungledev

22
如果您正在使用故事板,您可以设置导航栏的色调颜色。

enter image description here

enter image description here


正是我所需要的。谢谢! - Chad Lewis

11

在初始化navigationController的rootViewController中,我将以下代码放在了viewDidAppear方法中:

//set back button color
[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], UITextAttributeTextColor,nil] forState:UIControlStateNormal];
//set back button arrow color
[self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];

2
UITextAttributeTextColor在iOS 7中已被弃用 - 请使用NSForegroundColorAttributeName。 - amergin

7

2
这就是它!将其放在应用程序代理中。非常好用!window.tintColor = [UIColor blackColor]; // 我想要黑色 - Marc Watson

6

您可以在按钮(或栏按钮项)或视图控制器的视图上设置tintColor属性。默认情况下,该属性将继承父视图的色调,一直到您的应用程序顶层UIWindow


1
感谢回复。然而,在iOS 7上它的工作方式有些不同。我的旧代码使用tintColor,但是iOS 7似乎并不太喜欢它。最终,我使用了官方的苹果开发iOS 7过渡指南来解决一些UI问题。 - kevinl
请查看我的答案,其中有一个离散的示例,展示如何实现这一点:https://dev59.com/1GMl5IYBdhLWcg3wc2xp#18809412 - DiscDev

5

更新Swift 3版本

navigationController?.navigationItem.rightBarButtonItem?.tintColor = UIColor.yellow
navigationController?.navigationBar.tintColor = UIColor.red
navigationController?.navigationBar.barTintColor = UIColor.gray
navigationController?.navigationBar.titleTextAttributes =  [NSForegroundColorAttributeName: UIColor.blue]

结果: 这里输入图片描述

5
UINavigationBar *nbar = self.navigationController.navigationBar;

if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_6_1) {
   //iOS 7
   nbar.barTintColor = [UIColor blueColor]; // bar color
   //or custom color 
   //[UIColor colorWithRed:19.0/255.0 green:86.0/255.0 blue:138.0/255.0 alpha:1];

   nbar.navigationBar.translucent = NO;

   nbar.tintColor = [UIColor blueColor]; //bar button item color

} else {
   //ios 4,5,6
   nbar.tintColor = [UIColor whiteColor];
   //or custom color
   //[UIColor colorWithRed:19.0/255.0 green:86.0/255.0 blue:138.0/255.0 alpha:1];

}

太好了。它帮助了我。谢谢。 - Yogesh Lolusare

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接