how to change the color of backBarButtonItem?

给你一囗甜甜゛ 提交于 2019-12-06 04:12:53

问题


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.


回答1:


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



回答2:


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:

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.




回答3:


Praveen-K answer is right, but take in mind that you'll have to do it in every viewcontroller.

Starting at iOS5, Apple have introduced the "appearance" concept.

- (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.




回答4:


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



来源:https://stackoverflow.com/questions/7497543/how-to-change-the-color-of-backbarbuttonitem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!