how to change the color of backBarButtonItem?

2020-07-18 04:27发布

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.

4条回答
Luminary・发光体
2楼-- · 2020-07-18 04:40

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

查看更多
等我变得足够好
3楼-- · 2020-07-18 04:44
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];
查看更多
我只想做你的唯一
4楼-- · 2020-07-18 04:45

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.

查看更多
戒情不戒烟
5楼-- · 2020-07-18 04:54

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.

查看更多
登录 后发表回答