自定义“按下”UIBarButtonItem背景

我试图为我的UIBarButtonItem设置自定义按下的图像,但似乎没有任何工作。 根据我的理解,下面的代码应该可以工作,但是尽pipe为高亮显示的状态设置了图像,按下button时看起来完全一样。

有任何想法吗?

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

我认为你应该创build一个custom UIButton背景图像为不同的控制状态匹配你的配色scheme,然后使用这个UIButton作为custom UIBarButtonItem的视图。

 UIButton *customButton = [UIButton buttonWithType:...]; [customButton setBackgroundImage:barButtonBackground forState:UIControlStateNormal]; [customButton setBackgroundImage:barButtonPressedBackground forState:UIControlStateSelected]; UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView: customButton]; 

如果你想要一些其他的参考,那么你可以通过这个美丽的链接: 使用外观代理风格的应用程序

你可以尝试这样的。 为我工作。

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

也许这将帮助你(只需将该文件添加到项目中):

的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 

一些视图控制器:

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