I would like to create a NavigationBar with images as buttons on the right side of the NavigationBar.
Something like below Snapshot
I use a category for this:
UIBarButtonItem+WithImageOnly.h:
#import
@interface UIBarButtonItem (WithImageOnly)
- (id)initWithImageOnly:(UIImage*)image target:(id)target action:(SEL)action;
@end
UIBarButtonItem+WithImageOnly.m:
#import "UIBarButtonItem+WithImageOnly.h"
@implementation UIBarButtonItem (WithImageOnly)
- (id)initWithImageOnly:(UIImage *)image target:(id)target action:(SEL)action {
CGRect frame = CGRectMake(0, 0, image.size.width, image.size.height);
frame = CGRectInset(frame, -5, 0);
UIButton *button = [[UIButton alloc] initWithFrame:frame];
[button setImage:image forState:UIControlStateNormal];
[button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
return [self initWithCustomView:button];
}
@end
Usage:
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithImageOnly:[UIImage imageNamed:@"image"] target:self action:@selector(someAction:)]