只需要写一个 UIBarButtonItem类的扩展 其实原理很简单
扩展类创建就不在描述了 下面只贴上方法的使用
扩展类.h
+ (instancetype)itemWithNorImage:(NSString *)image highImage:(NSString *)highImage target:(id)target action:(SEL)action andFrame:(CGRect)rect;
扩展类.m
+ (instancetype)itemWithNorImage:(NSString *)image highImage:(NSString *)highImage target:(id)target action:(SEL)action andFrame:(CGRect)rect
{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setImage:[UIImage imageNamed:image] forState:UIControlStateNormal];
[btn setImage:[UIImage imageNamed:highImage] forState:UIControlStateHighlighted];
btn.frame = rect;
[btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
UIView *containVew = [[UIView alloc] initWithFrame:btn.bounds];
[containVew addSubview:btn];
return [[UIBarButtonItem alloc]initWithCustomView:containVew];
}
其实原理就是将这个Button 放在一个View上 再把这个View 放在UIBarButtonItem上,
而以前则是直接将Button放在Item上 会出现一些图片变形问题。
使用
单一左,或者右
UIBarButtonItem *item1 = [UIBarButtonItem itemWithNorImage:@"123.png" highImage:nil target:self action:@selector(gogo) andFrame:CGRectMake(0,0,40,40)];
[self.navigationItem setLeftBarButtonItem:item1];
多个item