封裝導(dǎo)航欄左右兩邊帶圖片的UIBarButtonItem
- 都是封裝的思想
- 創(chuàng)建一個類別繼承自UIBarButtonItem
- 寫一個類方法
- 實現(xiàn)這個方法
- 代碼如下
/**
* 快速創(chuàng)建一個顯示圖片的item
*
* @param action 監(jiān)聽方法
*/
+ (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]; }
- 調(diào)用方法如下
// 左邊按鈕 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)];