Skip to content

Instantly share code, notes, and snippets.

@ktakayama
Created May 8, 2012 06:42
Show Gist options
  • Save ktakayama/2633081 to your computer and use it in GitHub Desktop.
Save ktakayama/2633081 to your computer and use it in GitHub Desktop.
#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
Copy link

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