Create a gist now

Instantly share code, notes, and snippets.

#import <Foundation/Foundation.h>
@interface UIBarButtonItem (appearance)
+ (void) setupAppearance;
@end
#import "UIBarButtonItem+appearance.h"
#import <objc/runtime.h>
@implementation UIBarButtonItem (appearance)
+ (void) setupAppearance {
[[UIBarButtonItem appearance] setBackgroundImage:[UIImage imageNamed:@"bar_button"]
forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[[UIBarButtonItem appearance] setBackgroundImage:[UIImage imageNamed:@"bar_button_hl"]
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:@"bar_button_save"]
forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[self setBackgroundImage:[UIImage imageNamed:@"bar_button_save_hl"]
forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
} else {
[self setBackgroundImage:[UIImage imageNamed:@"bar_button"]
forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[self setBackgroundImage:[UIImage imageNamed:@"bar_button_hl"]
forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
}
}
@end
@mtvv
mtvv commented Oct 9, 2012

Thank you so much for this. Have been looking for hours to find a way to keep the behaviour of the UIBarButtonItemStyleDone buttons.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment