UIBarButtonItem with custom image and no border

前端 未结 9 672
旧巷少年郎
旧巷少年郎 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 <UIKit/UIKit.h>
    
    @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
    
    0 讨论(0)
  • 2020-12-12 13:07

    One another solution, think it's simpler in case when creating button programatically:

    UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithImage:defaultImage
                                                 landscapeImagePhone:landscapeImage
                                                               style:UIBarButtonItemStylePlain
                                                              target:self
                                                              action:@selector(someSelector)];
    [button setBackgroundImage:[UIImage new] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
    [button setBackgroundImage:[UIImage new] forState:UIControlStateNormal barMetrics:UIBarMetricsLandscapePhone];
    
    0 讨论(0)
  • 2020-12-12 13:09

    You can add a method to UIBarButtonItem without subclassing it using custom category:

    @interface UIBarButtonItem(MyCategory)
    
    + (UIBarButtonItem*)barItemWithImage:(UIImage*)image target:(id)target action:(SEL)action;
    
    @end
    
    @implementation UIBarButtonItem(MyCategory)
    
    + (UIBarButtonItem*)barItemWithImage:(UIImage*)image target:(id)target action:(SEL)action{
     // Move your item creation code here
    }
    @end
    

    So anywhere in your code you can create bar item calling this method (provided that you include a header with its declaration).

    P.S. You do not need to use 'v' UIView as you can create UIBarButtonItem with a button as custom view directly.
    P.P.S. You also need [forward release] in your code.

    0 讨论(0)
提交回复
热议问题