iOS 7 UIBarButton 后退按钮箭头颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18384488/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 00:49:02  来源:igfitidea点击:

iOS 7 UIBarButton back button arrow color

iosios7

提问by kevinl

I'm trying to change the back button arrow

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

enter image description here

在此处输入图片说明

I'm currently using the following to control the text size as well as the text color on the back button:

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

[[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];

but if I want to change only the arrow's color for the back button, what should i do?

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

回答by DiscDev

To change the back button chevron color for a specific navigation controller*:

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

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

*If you are using an app with more than 1 navigation controller, and you want this chevron color to apply to each, you may want to use the appearance proxy to set the back button chevron for every navigation controller, as follows:

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

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

And for good measure, in swift (thanks to Jay Mayu in the comments):

为了很好的衡量,迅速(感谢评论中的 Jay Mayu):

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

回答by Bart van Kuik

You have to set the tintColor of the entire app.

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

self.window.tintColor = [UIColor redColor];

Or in Swift 3:

或者在 Swift 3 中:

self.window?.tintColor = UIColor.blue

Source: iOS 7 UI Transition Guide

来源:iOS 7 UI 过渡指南

回答by David Castro

You can set the color on the entire app navigation's bar using the method

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

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

回答by selmad

It is possible to change only arrow's color (not back button title's color) on this way:

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

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

Navigation bar contains subview of _UINavigationBarBackIndicatorView type (last item in subviews array) which represents arrow.

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

Result is navigation bar with different colors of back button arrow and back button title

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

回答by ThE uSeFuL

If you are using storyboards you could set the navigation bar tint colour.

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

enter image description here

在此处输入图片说明

enter image description here

在此处输入图片说明

回答by John Riselvato

Inside the rootViewController that initializes the navigationController, I put this code inside my viewDidAppear method:

在初始化 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]];

回答by Chamath Jeevan

In iOS 6, tintColor tinted the background of navigation bars, tab bars, toolbars, search bars, and scope bars. To tint a bar background in iOS 7, use the barTintColor property instead.

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

iOS 7 Design Resources iOS 7 UI Transition Guide

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

回答by Mike Weller

You can set the tintColorproperty on the button (or bar button item) or the view controller's view. By default, the property will inherit the tint from the parent view, all the way up to the top level UIWindowof your app.

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

回答by Márcia Silva

I had to use both:

我不得不同时使用两者:

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

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

And works for me, thank you for everyone!

对我有用,谢谢大家!

回答by MaxEcho

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];

}