CustomBarButtonItem, navigationItem,图片拉伸

1.图片拉伸resizableImageWithCapInsets

   /* iOS 5.0  弃用stretchableImageWithLeftCapWidth  改用 resizableImageWithCapInsets 来将图片拉伸 */

UIImage *bg = [[UIImage imageNamed:@"navigationbar_title_highlighted"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 10, 120,44)]; // UIImage *bg = [[UIImage imageNamed:@"navigationbar_title_highlighted"] stretchableImageWithLeftCapWidth:10 topCapHeight:0];

2.设置navigationItem

    self.navigationItem.titleView = btn;
    self.navigationItem.rightBarButtonItem = [[CustomBarButtonItem alloc] initBarButtonWithImage:[UIImage imageNamed:@"navigationbar_refresh"]
                                                                                          target:self
                                                                                     action:@selector(reloadList)];

3.

navigationItem中自定义的CustomBarButtonItem

 

-(id)initBarButtonWithImage:(UIImage *)image  target:(id)target action:(SEL)action
{
    self = [super init];
    if (self != nil) {
        UIFont *textFont = [UIFont boldSystemFontOfSize:14.0f];
        CGSize size = image.size;
        CGFloat width = MAX(size.width, 40);
        UIImage *bgImg = [[UIImage imageNamed:@"nav-bar-button"] stretchableImageWithLeftCapWidth:9 topCapHeight:0];
        UIImage *bgImgPressed = [[UIImage imageNamed:@"nav-bar-button-pressed"] stretchableImageWithLeftCapWidth:9 topCapHeight:0];
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        
        button.frame = CGRectMake(0, 0, width, bgImg.size.height);
        
        [button setBackgroundImage:bgImg forState:UIControlStateNormal];
        [button setBackgroundImage:bgImgPressed forState:UIControlStateHighlighted];
        
        [button setImage:image forState:UIControlStateNormal];
        [button setImage:image forState:UIControlStateHighlighted];
        
        button.titleLabel.font = textFont;
        
        [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
        
        self.customView = button;
        
    }
    
    return self;
}

 

posted @ 2013-02-19 15:36  不想当元帅的好兵。  阅读(324)  评论(0编辑  收藏  举报