UIBarButtonItem自定义导航条按钮

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

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

.h 文件

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

@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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容