只需要寫一個?UIBarButtonItem類的擴(kuò)展 其實原理很簡單
擴(kuò)展類創(chuàng)建就不在描述了 下面只貼上方法的使用
擴(kuò)展類.h
+ (instancetype)itemWithNorImage:(NSString *)image highImage:(NSString *)highImage target:(id)target action:(SEL)action andFrame:(CGRect)rect;
擴(kuò)展類.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上 ?會出現(xiàn)一些圖片變形問題。
使用
單一左堵未,或者右
UIBarButtonItem *item1 = [UIBarButtonItem itemWithNorImage:@"123.png" highImage:nil target:self action:@selector(gogo) andFrame:CGRectMake(0,0,40,40)];
[self.navigationItem setLeftBarButtonItem:item1];
多個item