Skip to content

Instantly share code, notes, and snippets.

@kreeger
Created December 14, 2012 20:54
Show Gist options
  • Save kreeger/4288582 to your computer and use it in GitHub Desktop.
Save kreeger/4288582 to your computer and use it in GitHub Desktop.
Creates a UIButton with a custom view that mimics the style of a UIBarButtonSystemItem.
#import <UIKit/UIKit.h>
@interface UIButton (Convenience)
+ (id)buttonWithBarItemStyleAndCustomView:(UIView *)customView;
@end
@implementation UIButton (Convenience)
+ (id)buttonWithBarItemStyleAndCustomView:(UIView *)customView {
UIButton *button = [self buttonWithType:UIButtonTypeCustom];
UIImage *back = [[UIImage imageNamed:@"UINavigationBarBlackTranslucentButton.png"]
stretchableImageWithLeftCapWidth:5.0
topCapHeight:0.0];
UIImage *pressed = [[UIImage imageNamed:@"UINavigationBarBlackTranslucentButtonPressed.png"]
stretchableImageWithLeftCapWidth:5.0
topCapHeight:0.0];
button.frame = CGRectMake(0, 0, 34.0f, 30.0f);
[button setBackgroundImage:back forState:UIControlStateNormal];
[button setBackgroundImage:pressed forState:UIControlStateSelected];
[button setBackgroundImage:pressed forState:UIControlStateHighlighted];
[button addSubview:customView];
customView.userInteractionEnabled = NO;
customView.frame = CGRectCenterRectInRect(customView.frame, button.frame);
return button;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment