微博(三)封装UIBarButtonItem

2016-01-13  本文已影响120人  破夕_____________

封装导航栏左右两边带图片的UIBarButtonItem


/**
*  快速创建一个显示图片的item
*
*  @param action   监听方法
*/

+ (UIBarButtonItem *)itemWithIcon:(NSString *)icon highIcon:(NSString *)highIcon target:(id)target action:(SEL)action;


+ (UIBarButtonItem *)itemWithIcon:(NSString *)icon highIcon:(NSString *)highIcon target:(id)target action:(SEL)action { UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; [button setBackgroundImage:[UIImage imageWithName:icon] forState:UIControlStateNormal]; [button setBackgroundImage:[UIImage imageWithName:highIcon] forState:UIControlStateHighlighted]; button.frame = (CGRect){CGPointZero, button.currentBackgroundImage.size}; [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside]; return [[UIBarButtonItem alloc] initWithCustomView:button]; }


// 左边按钮 self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithIcon:@"navigationbar_friendsearch" highIcon:@"navigationbar_friendsearch_highlighted" target:self action:@selector(findFriend)]; // 右边按钮 self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithIcon:@"navigationbar_pop" highIcon:@"navigationbar_pop_highlighted" target:self action:@selector(pop)];


上一篇 下一篇

猜你喜欢

热点阅读