Skip to content

Instantly share code, notes, and snippets.

@arnaudbos
Created October 4, 2011 13:13
Show Gist options
  • Save arnaudbos/1261613 to your computer and use it in GitHub Desktop.
Save arnaudbos/1261613 to your computer and use it in GitHub Desktop.
Create a UIBarButtonItem with a red (or other image) background
http://i.stack.imgur.com/P0xJO.png
UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
[cancelButton setBackgroundImage:[UIImage imageNamed:@"delete.png"] forState:UIControlStateNormal];
[cancelButton setTitle:@"Annuler" forState:UIControlStateNormal];
cancelButton.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:12.0f];
[cancelButton.layer setCornerRadius:4.0f];
[cancelButton.layer setMasksToBounds:YES];
[cancelButton.layer setBorderWidth:1.0f];
[cancelButton.layer setBorderColor: [[UIColor grayColor] CGColor]];
cancelButton.frame=CGRectMake(0.0, 100.0, 60.0, 30.0);
[cancelButton addTarget:self action:@selector(clickOnCancelButton) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem* cancelButtonItem = [[UIBarButtonItem alloc] initWithCustomView:cancelButton];
[self.navigationItem setLeftBarButtonItem:cancelButtonItem animated:TRUE];
[cancelButtonItem release];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment