Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIBarButtonItem with color?

Is it possible to have a red UIBarButtonItem?

like image 302
4thSpace Avatar asked Mar 20 '09 03:03

4thSpace


3 Answers

If anyone is looking for code to exactly duplicate a simple UIBarButtonItem:

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundImage:[UIImage imageNamed:@"delete.png"] forState:UIControlStateNormal];
[button setTitle:@"Delete" forState:UIControlStateNormal];
button.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:12.0f];
[button.layer setCornerRadius:4.0f];
[button.layer setMasksToBounds:YES];
[button.layer setBorderWidth:1.0f];
[button.layer setBorderColor: [[UIColor grayColor] CGColor]];
button.frame=CGRectMake(0.0, 100.0, 60.0, 30.0);
[button addTarget:self action:@selector(batchDelete) forControlEvents:UIControlEventTouchUpInside];

UIBarButtonItem* deleteItem = [[UIBarButtonItem alloc] initWithCustomView:button];

And delete.png is:

delete.png

like image 155
Scott Montgomerie Avatar answered Oct 29 '22 05:10

Scott Montgomerie


Why not just :

[[self editButtonItem] setTintColor:[UIColor redColor]];

(on sdk 4.* , ios 5 and up)

like image 36
frinkr Avatar answered Oct 29 '22 05:10

frinkr


You can create custom UIButton with your desired look and initialize your UIBarButtonItem with it as a custom view.

UIButton *button = [UIButton buttonWithType:....];
...(customize your button)...

UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView:button];
like image 27
Marcus Foster Avatar answered Oct 29 '22 05:10

Marcus Foster