Skip to content

Instantly share code, notes, and snippets.

@florieger
Created August 3, 2011 12:56
Show Gist options
  • Save florieger/1122564 to your computer and use it in GitHub Desktop.
Save florieger/1122564 to your computer and use it in GitHub Desktop.
Custom UIBarButtonItem
#import <QuartzCore/QuartzCore.h>
- (UIBarButtonItem*)barButtonWithTitle:(NSString*)title target:(id)target action:(SEL)action
{
// Start with a UIButton, because a UIBarButton can't be modified.
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
// Modify layer
[button.layer setCornerRadius:5.0f];
[button.layer setMasksToBounds:YES];
[button.layer setBorderWidth:1.0f];
[button.layer setBorderColor: [[UIColor colorWithWhite:0.4 alpha:1.0] CGColor]];
button.frame=CGRectMake(0.0, 0.0, 70.0, 30.0);
// Set background
[button setBackgroundImage:[UIImage imageNamed:@"ButtonBG.png"] forState:UIControlStateNormal];
// Set title
[button setTitle:title forState:UIControlStateNormal];
[button setTitleColor:[UIColor colorWithWhite:0.2 alpha:1.0] forState:UIControlStateNormal];
[button setTitleColor:[UIColor colorWithWhite:1.0 alpha:1.0] forState:UIControlStateHighlighted];
button.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:12.0f];
// Add action
[button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
// Build UIBarButtonItem with custom view
return [[UIBarButtonItem alloc] initWithCustomView:button];
}
@florieger
Copy link
Author

Simple method for custom UIBarButtonItem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment