iOS 7 UIBarButton后退按钮箭头颜色

kev*_*inl 172 ios ios7

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

在此输入图像描述

我目前正在使用以下内容来控制文本大小以及后退按钮上的文本颜色:

[[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];
Run Code Online (Sandbox Code Playgroud)

但如果我只想改变后退按钮的箭头颜色,我该怎么办?

Dis*_*Dev 438

要更改特定导航控制器的后退按钮V形颜色*:

self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
Run Code Online (Sandbox Code Playgroud)

*如果您使用的应用程序具有多个导航控制器,并且您希望将此V形颜色应用于每个,您可能需要使用外观代理为每个导航控制器设置后退按钮V形符号,如下所示:

[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
Run Code Online (Sandbox Code Playgroud)

在好的情况下,迅速(感谢Jay Mayu在评论中):

UINavigationBar.appearance().tintColor = UIColor.whiteColor()
Run Code Online (Sandbox Code Playgroud)

  • 对于swift`UINavigationBar.appearance().tintColor = UIColor.whiteColor()` (7认同)
  • 如果您为整个应用程序使用相同的导航控制器,这将仅适用于整个应用程序.我实际上建议巴特的建议. (3认同)
  • @ jcampbell1 - 我在我的几个应用程序中成功使用了这个...也许你没有读过关于多个UINavigationControllers的警告. (2认同)

Bar*_*uik 57

您必须设置整个应用程序的tintColor.

self.window.tintColor = [UIColor redColor];
Run Code Online (Sandbox Code Playgroud)

或者在Swift 3中:

self.window?.tintColor = UIColor.blue
Run Code Online (Sandbox Code Playgroud)

来源:iOS 7 UI过渡指南

  • 如果您只想设置后退按钮V形符号(而不是整个应用程序)的颜色,如原始问题中所述,这是错误的答案.请在此处查看我的答案以获取正确答案:http://stackoverflow.com/a/18809412/1103584 (4认同)

小智 55

您可以使用该方法在整个应用程序导航栏上设置颜色

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:
(NSDictionary *)launchOptions{
    [[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
}
Run Code Online (Sandbox Code Playgroud)


小智 23

可以通过以下方式仅更改箭头的颜色(不是后退按钮标题的颜色):

[[self.navigationController.navigationBar.subviews lastObject] setTintColor:[UIColor blackColor]];
Run Code Online (Sandbox Code Playgroud)

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

结果是导航栏,具有不同颜色的后退按钮箭头和后退按钮标题

  • 根据系统视图的子视图的顺序是一个坏主意. (4认同)
  • 这工作,当设置self.navigationController.navigationBar.tintColor不起作用. (2认同)

ThE*_*FuL 22

如果您使用的是故事板,则可以设置导航栏的色调.

在此输入图像描述

在此输入图像描述


Joh*_*ato 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]];
Run Code Online (Sandbox Code Playgroud)

  • 在iOS 7中不推荐使用UITextAttributeTextColor - 使用NSForegroundColorAttributeName (2认同)

Cha*_*van 7

在iOS 6中,tintColor着色了导航栏,标签栏,工具栏,搜索栏和范围栏的背景.要在iOS 7中着色条形背景,请改用barTintColor属性.

iOS 7设计资源iOS 7 UI过渡指南

  • 这是一个!...把它放在app委托中.奇迹般有效!window.tintColor = [UIColor blackColor]; //我想要黑色 (2认同)

Mik*_*ler 6

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


小智 5

我不得不同时使用两者:

[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil] 
                     setTitleTextAttributes:[NSDictionary 
               dictionaryWithObjectsAndKeys:[UIColor whiteColor], UITextAttributeTextColor,nil] 
                                   forState:UIControlStateNormal];

[[self.navigationController.navigationBar.subviews lastObject] setTintColor:[UIColor whiteColor]];
Run Code Online (Sandbox Code Playgroud)

并为我工作,谢谢大家!


Sal*_*lim 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];

}
Run Code Online (Sandbox Code Playgroud)


Ric*_*tti 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]
Run Code Online (Sandbox Code Playgroud)

结果: 在此处输入图片说明