IOS 5 如何更改导航栏中后退按钮的颜色?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7929382/
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
IOS 5 How to change the color of back button in a navigation bar?
提问by Nilesh Tupe
I want to change the color of back button of a navigation bar to make it look like this
我想更改导航栏后退按钮的颜色,使其看起来像这样
回答by Jacob Relkin
Set the backBarButtonItem
's tintColor
:
设置backBarButtonItem
的tintColor
:
self.navigationItem.backBarButtonItem.tintColor = [UIColor redColor];
TIP: If you want this to be applied to allUIBarButtonItem
instances in your application by default, then you can use the new UIAppearance
API to do just that:
提示:如果您希望默认情况下将此应用于应用程序中的所有UIBarButtonItem
实例,那么您可以使用新的UIAppearance
API 来做到这一点:
[[UIBarButtonItem appearance] setTintColor:[UIColor redColor]];
回答by svn
The first line of jacob's answer didn't work for me because the backBarButtonItem was NULL. It is NULL because it's been created later automatically when switching to an other ViewController. At that time you can set the title of the button with
雅各布回答的第一行对我不起作用,因为 backBarButtonItem 为 NULL。它是 NULL,因为它是稍后切换到其他 ViewController 时自动创建的。那时你可以设置按钮的标题
self.title = @"nice title"; // self is here the view(controller) within the popoverController
but you can't set the tintColor.
但你不能设置 tintColor。
What worked for me, was to create a new UIBarButtonItem without any style. Then set the title and color propertys and set it as backBarButtonItem.
对我有用的是创建一个没有任何样式的新 UIBarButtonItem。然后设置 title 和 color 属性并将其设置为 backBarButtonItem。
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] init];
backButton.title = @"go back - now!";
backButton.tintColor = [UIColor colorWithRed:0.1 green:0.5 blue:0.7 alpha:1.0];
self.navigationItem.backBarButtonItem = backButton;
[okButton release];
回答by Andreas Ley
If you want to make the button look exactly like in your picture, you may use an image, too:
如果你想让按钮看起来和你的图片完全一样,你也可以使用图像:
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:[UIImage imageNamed:@"back_button_bg"]
forState:UIControlStateNormal
barMetrics:UIBarMetricsDefault];
The background image must be a resizable image for good results.
背景图像必须是可调整大小的图像才能获得良好的效果。
回答by Zeev Vax
Best way I found to set it globally or locally is
我发现在全局或本地设置它的最佳方法是
[[UIBarItem appearance] setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor colorWithRed:220.0/255.0 green:104.0/255.0 blue:1.0/255.0 alpha:1.0], UITextAttributeTextColor,
[UIColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0], UITextAttributeTextShadowColor,
[NSValue valueWithUIOffset:UIOffsetMake(0, 1)], UITextAttributeTextShadowOffset,
[UIFont fontWithName:@"AmericanTypewriter" size:0.0], UITextAttributeFont,
nil]
forState:UIControlStateNormal];
回答by Virus
[[UINavigationBar appearance]setTintColor:[UIColor whiteColor]];
try this It is working for me...
试试这个它对我有用......