xcode 如何更改 backBarButtonItem 的颜色?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7497543/
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
how to change the color of backBarButtonItem?
提问by iPhone
In my application I want to change the color of bacBarButtonItem. Is it possible to change the color? or I have to put an image of it. and in case of image tell me the code how to put the image.
在我的应用程序中,我想更改 bacBarButtonItem 的颜色。是否可以更改颜色?或者我必须把它的图像。如果是图像,请告诉我如何放置图像的代码。
采纳答案by Praveen-K
UIImage *image = [UIImage imageNamed:@"imageName.png"];
UIBarButtonItem* backBarButton = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(backButtonAction)];
self.navigationItem.leftBarButtonItem=backBarButton;
[backBarButton release];
回答by SirRupertIII
If you just want to change the color you can do it with this line of code.
如果你只是想改变颜色,你可以用这行代码来完成。
[[UIBarButtonItem appearance] setTintColor:[UIColor redColor]];
Replace redColor with the following to adjust the color of the buttons:
将 redColor 替换为以下内容以调整按钮的颜色:
colorWithRed:0/255.0 green:144/255.0 blue:200/255.0 alpha:1.0// pick your color using this
Be sure to put this in the view controller that pushes. Not the view controller where you want to see this back button color.
一定要把它放在推送的视图控制器中。不是您想要看到此后退按钮颜色的视图控制器。
回答by Yorxxx
Praveen-K answer is right, but take in mind that you'll have to do it in every viewcontroller.
Praveen-K 的答案是正确的,但请记住,您必须在每个视图控制器中都这样做。
Starting at iOS5, Apple have introduced the "appearance" concept.
从iOS5开始,苹果就引入了“外观”的概念。
- (void)setBackButtonBackgroundImage:(UIImage *)backgroundImage forState:(UIControlState)state barMetrics:(UIBarMetrics)barMetrics __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_5_0) UI_APPEARANCE_SELECTOR;
In your case would be something like this
在你的情况下会是这样的
UIImage *image = [UIImage imageNamed:@"imageName.png"];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:image forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
But as I said, Praveen-K answer is ok and will work, but just to let you know for the future.
但正如我所说,Praveen-K 的答案是可以的,并且会起作用,但只是为了让您知道将来。
回答by BB.
Another way to change the color of back bar button item is to use segment control
另一种改变后退栏按钮项目颜色的方法是使用段控件
UISegmentedControl *button = [[[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"Back", nil]] autorelease];
button.frame = CGRectMake(0, 0, 60, 30);
button.center = self.view.center;
button.momentary = YES;
button.segmentedControlStyle = UISegmentedControlStyleBar;
button.tintColor = [UIColor colorWithRed:0 green:0.1 blue:0.5 alpha:0];
[button addTarget:self action:@selector(handleBack:) forControlEvents:UIControlEventValueChanged];
UIBarButtonItem *backButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
Notice that we assign the color we want to the property tintColor of UISegmentedControl. I got the idea from this site: http://charles.lescampeurs.org/2011/02/10/tint-color-uibutton-and-uibarbuttonitem
请注意,我们将我们想要的颜色分配给 UISegmentedControl 的属性 tintColor。我从这个网站得到了这个想法:http: //charles.lescampeurs.org/2011/02/10/tint-color-uibutton-and-uibarbuttonitem