I know how to customize UIBarButtonItem
using -setBackgroundImage: forState: barMetrics:
, but I would like to use different images for UIBarButtonItemStyleDone
and UIBarButtonItemStylePlain
.
Is there a way to accomplish this using the UIAppearance
protocol? Or do I have to set the image each time I want a "Done" style button?
(I tried messing around with code like the following:
[[UIBarButtonItem appearance] setBackgroundImage:image forState:UIControlStateNormal barMetrics:UIBarButtonItemStyleDone];
But that just sets every bar button with the "Done" image.)
Thanks!
In iOS 6 you can use the new method of UIBarButtonItem class:
- (void)setBackgroundImage:(UIImage *)backgroundImage
forState:(UIControlState)state
style:(UIBarButtonItemStyle)style
barMetrics:(UIBarMetrics)barMetrics
It sets the background image for the specified state, style, and metrics. More details are available in the Apple docs
So to change the appearance of all UIBarButtonItems you can use something like:
UIImage *doneBackgroundImage = [[UIImage imageNamed:@"button_done.png"]
resizableImageWithCapInsets:UIEdgeInsetsMake(0, 4, 0, 4)];
[[UIBarButtonItem appearance] setBackgroundImage:doneBackgroundImage
forState:UIControlStateNormal
style:UIBarButtonItemStyleDone
barMetrics:UIBarMetricsDefault];
for IOS5*
The only way I found is using a UIBarButtonItem category:
UIBarButtonItem+Appearance.h
#import <Foundation/Foundation.h>
@interface UIBarButtonItem (Appearance)
+ (void) setupAppearance;
@end
UIBarButtonItem+Appearance.m
#import "UIBarButtonItem+Appearance.h"
#import <objc/runtime.h>
@implementation UIBarButtonItem (Appearance)
+ (void) setupAppearance {
[[UIBarButtonItem appearance] setBackgroundImage: [[UIImage imageNamed:@"customButton"]
resizableImageWithCapInsets: UIEdgeInsetsMake(8, 8, 8, 8)]
forState: UIControlStateNormal
barMetrics: UIBarMetricsDefault];
[[UIBarButtonItem appearance] setBackgroundImage: [[UIImage imageNamed:@"customButtonHiglhighted"]
resizableImageWithCapInsets: UIEdgeInsetsMake(8, 8, 8, 8)]
forState: UIControlStateHighlighted
barMetrics: UIBarMetricsDefault];
Class klass = objc_getClass("UIBarButtonItem");
Method targetMethod = class_getInstanceMethod(klass, @selector(setStyle:));
Method newMethod = class_getInstanceMethod(klass, @selector(__setStyle:));
method_exchangeImplementations(targetMethod, newMethod);
}
- (void) __setStyle:(UIBarButtonItemStyle)style {
[self __setStyle:style];
if(style == UIBarButtonItemStyleDone) {
[self setBackgroundImage:[[UIImage imageNamed:@"customDoneButton"] resizableImageWithCapInsets: UIEdgeInsetsMake(8, 8, 8, 8)]
forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[self setBackgroundImage:[UIImage imageNamed:@"customDoneButtonClicked"]
forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
} else {
[self setBackgroundImage:[[UIImage imageNamed:@"customButton"] resizableImageWithCapInsets: UIEdgeInsetsMake(8, 8, 8, 8)]
forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[self setBackgroundImage:[UIImage imageNamed:@"customButtonHighlighted"]
forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
}
}
@end
Hope this is what you are looking for. Credit for this solution goes to https://gist.github.com/2633081
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With