Custom “Pressed” UIBarButtonItem Backgrounds

2020-02-10 06:47发布

问题:

I'm trying to set a custom pressed image for my UIBarButtonItem but nothing seems to be working. From my understanding, the code below should work, but despite setting the image for the highlighted state, the button looks exactly the same when pressed.

Any ideas?

UIImage *barButtonBackground = [[UIImage imageNamed:ANBarButtonItemBackgroundImageName] resizableImageWithCapInsets:UIEdgeInsetsMake(5.0f,5.0f, 5.0f, 5.0f)];
UIImage *barButtonPressedBackground = [[UIImage imageNamed:ANBarButtonPressedImageName] resizableImageWithCapInsets:UIEdgeInsetsMake(5.0f,5.0f, 5.0f, 5.0f)];

[self.navigationItem.leftBarButtonItem setBackgroundImage:barButtonBackground forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[self.navigationItem.rightBarButtonItem setBackgroundImage:barButtonBackground forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

[self.navigationItem.leftBarButtonItem setBackgroundImage:barButtonPressedBackground forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
[self.navigationItem.rightBarButtonItem setBackgroundImage:barButtonPressedBackground forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];

回答1:

I think you should create a custom UIButton with background images for the different control states that match your color scheme, then use this UIButton as the view for a custom UIBarButtonItem.

UIButton *customButton = [UIButton buttonWithType:...];

[customButton setBackgroundImage:barButtonBackground  forState:UIControlStateNormal];
[customButton setBackgroundImage:barButtonPressedBackground  forState:UIControlStateSelected];

UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView: customButton];

If you want some other Reference then you can go through this beautiful link : Using Appearance Proxy to Style Apps



回答2:

You can probably try like this. Works for me.

UIImage* image3 = [UIImage imageNamed:@"mail-48_24.png"];
CGRect frameimg = CGRectMake(0, 0, image3.size.width, image3.size.height);
UIButton *someButton = [[UIButton alloc] initWithFrame:frameimg];
[someButton setBackgroundImage:image3 forState:UIControlStateNormal];
[someButton addTarget:self action:@selector(sendmail)
     forControlEvents:UIControlEventTouchUpInside];
[someButton setShowsTouchWhenHighlighted:YES];

UIBarButtonItem *mailbutton =[[UIBarButtonItem alloc] initWithCustomView:someButton];
self.navigationItem.rightBarButtonItem=mailbutton;
[someButton release];


回答3:

Maybe this will help you (just add that files into project):

UIBarButtonItem+customLook.h

#import <UIKit/UIKit.h>

@interface UIBarButtonItem (customLook)

+ (UIBarButtonItem *)barButtonItemWithImage:(UIImage *)image selectedBackGroundImage:(UIImage *)selected target:(id)target action:(SEL)selector;

@end

UIBarButtonItem+customLook.m

#import "UIBarButtonItem+customLook.h"

@implementation UIBarButtonItem (customLook)

+ (UIBarButtonItem *)barButtonItemWithImage:(UIImage *)image selectedBackGroundImage:(UIImage *)selected target:(id)target action:(SEL)selector
{
    UIButton* someButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [someButton setImage:image forState:UIControlStateNormal];
    [someButton setBackgroundImage:selected forState:UIControlStateSelected];

    CGSize size = image.size;
    CGRect frame = CGRectZero;
    frame.size.width = size.width;
    frame.size.height = 30.0f; // Standart height
    someButton.frame = frame;

    [someButton addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];

    UIBarButtonItem* backButton = [[UIBarButtonItem alloc] initWithCustomView:someButton];

    return backButton;
}

@end

Some view controller:

#import "UIBarButtonItem+customLook.h"
...

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIBarButtonItem *bbi = [UIBarButtonItem barButtonItemWithImage:[UIImage imageNamed:@"29-heart-selected.png"]
                                          selectedBackGroundImage:[UIImage imageNamed:@"DarkButton_Pressed.png"]
                                                            target:self
                                                            action:@selector(someAction:)];
    self.navigationItem.rightBarButtonItem = bbi;
}

...

- (void)someAction:(id)sender
{
    [(UIButton *)sender setSelected:YES];
}


回答4:

    UIImage* btnImage = [UIImage imageNamed:@"share-icon-deselected-16x16.png"];
    CGRect frameimg = CGRectMake(0, 0, btnImage.size.width, btnImage.size.height);
    UIButton *someButton = [[UIButton alloc] initWithFrame:frameimg];
    [someButton setBackgroundImage:btnImage forState:UIControlStateNormal];
    [someButton addTarget:self action:@selector(share:)
         forControlEvents:UIControlEventTouchUpInside];
    [someButton setShowsTouchWhenHighlighted:YES];

    UIBarButtonItem *mailbutton =[[UIBarButtonItem alloc] initWithCustomView:someButton];
    self.navigationItem.rightBarButtonItem=mailbutton;




- (void)share:(id)sender{
    UIButton*btn = (UIButton*)sender;
    if (btn.isSelected)
    {
        [btn setSelected:NO];
        [btn setBackgroundImage:[UIImage imageNamed:@"share-icon-deselected-16x16.png"] forState:UIControlStateNormal];
    }
    else
    {
        [btn setSelected:YES];
        [btn setBackgroundImage:[UIImage imageNamed:@"share-icon-selected-16x16.png"] forState:UIControlStateNormal];
    }
};