UIBarButtonItem自定义导航条按钮

简单的自定义UIBarButtonItem按钮的代码片段

   UIBarButtonItem *leftBarButtonItem = [ConsumerButton itemWithImage:@"你的名字" highImage:@"你的名字" target:self action:@selector(你的点击事件)];
    [self.navigationItem setLeftBarButtonItem:leftBarButtonItem];

.h 文件

#import 
#import 

@interface ConsumerButton : NSObject

+ (UIBarButtonItem *)itemWithImage:(NSString *)image highImage:(NSString *)highImage target:(id)target action:(SEL)action;

@end

.m 文件

#import "ConsumerButton.h"

@implementation ConsumerButton
/**
 *  自定义导航栏右侧按钮
 */
+(UIBarButtonItem *)itemWithImage:(NSString *)image highImage:(NSString *)highImage target:(id)target action:(SEL)action {
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setBackgroundImage:[UIImage imageNamed:image] forState:UIControlStateNormal];
    [button setBackgroundImage:[UIImage imageNamed:highImage] forState:UIControlStateHighlighted];
    button.size= button.currentBackgroundImage.size;
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];

    return [[UIBarButtonItem alloc] initWithCustomView:button];
    
}
@end

你可能感兴趣的:(UIBarButtonItem自定义导航条按钮)