新浪微博客户端(3)-封装UIBarButtonItem

单独给NavigationBar上的两个NavigationItem设置图片显得比较麻烦,下面对创建单个UIBarButtonItem的过程进行封装。

UIBarButtonItem+Extension.m

#import "UIBarButtonItem+Extension.h"

@implementation UIBarButtonItem (Extension)


+ (UIBarButtonItem *)itemWithTarget:(id)target action:(SEL)action image:(NSString *)image highlighImage:(NSString *)highImage {


    
    // 自定义左上角按钮
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [backBtn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    // 设置图片
    [backBtn setBackgroundImage:[UIImage imageNamed:image] forState:UIControlStateNormal];
    [backBtn setBackgroundImage:[UIImage imageNamed:highImage] forState:UIControlStateHighlighted];
    // 设置尺寸
    backBtn.size = backBtn.currentBackgroundImage.size;

    return [[UIBarButtonItem alloc] initWithCustomView:backBtn];

}

@end

最终效果:

 

posted @ 2016-10-14 20:14  夜行过客  阅读(266)  评论(0编辑  收藏  举报