UIBarButtonItem with custom image and no border

前端 未结 9 689
旧巷少年郎
旧巷少年郎 2020-12-12 12:18

I want to create a UIBarButtonItem with a custom image, but I don\'t want the border that iPhone adds, as my Image has a special border.

It\'s the same as the back b

9条回答
  •  佛祖请我去吃肉
    2020-12-12 13:04

    Ok that category works very good because there are no problems with Popovercontroller :-)

    #import 
    
    @interface UIBarButtonItem (BarButtonItemExtended)
    + (UIBarButtonItem*)barItemWithImage:(UIImage*)image target:(id)target action:(SEL)action;
    -(void)performBarButtonAction:(id)sender;
    @end
    
    
    
    #import "UIBarButtonItem+BarButtonItemExtended.h"
    
    @implementation UIBarButtonItem (BarButtonItemExtended)
    
    + (UIBarButtonItem*)barItemWithImage:(UIImage*)image target:(id)target action:(SEL)action
    {    
        UIButton *imgButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [imgButton setImage:image forState:UIControlStateNormal];
        imgButton.frame = CGRectMake(0.0, 0.0, image.size.width, image.size.height);
    
        UIBarButtonItem *b = [[UIBarButtonItem alloc]initWithCustomView:imgButton];
    
        [imgButton addTarget:b action:@selector(performBarButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
        [b setAction:action];
        [b setTarget:target];
    
        return b;
    }
    
    -(void)performBarButtonAction:(UIButton*)sender
    {
        [[self target] performSelector:self.action withObject:self];
    }
    @end
    

提交回复
热议问题