Custom “Pressed” UIBarButtonItem Backgrounds

2020-02-10 06:10发布

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];

4条回答
三岁会撩人
2楼-- · 2020-02-10 06:44

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];
查看更多
Melony?
3楼-- · 2020-02-10 06:45

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

查看更多
欢心
4楼-- · 2020-02-10 06:55
    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];
    }
};
查看更多
SAY GOODBYE
5楼-- · 2020-02-10 07:02

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];
}
查看更多
登录 后发表回答