/**
* 快速创建一个显示图片的UIBarButtonItem
*/
+ (instancetype)itemWithTarget:(id)target action:(SEL)action icon:(NSString *)icon highIcon:(NSString *)highIcon
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundImage:[UIImage imageNamed:icon] forState:UIControlStateNormal];
[button setBackgroundImage:[[UIImage imageNamed:highIcon] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateHighlighted];
// 设置按钮尺寸
button.bounds = (CGRect){CGPointZero, button.currentBackgroundImage.size};
[button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
return [[self alloc] initWithCustomView:button];
}
- 注意:一定要把target传过去,否者self是UIBarButtonItem,找不着执行方法,会崩的
[[UIBarButtonItem alloc] initWithTitle:@"写私信" style:UIBarButtonItemStyleDone target:nil action:nil];
网友评论