public
Last active

  • Download Gist
UIBarButtonItem+appearance.h
Objective-C
1 2 3 4 5 6 7
#import <Foundation/Foundation.h>
 
@interface UIBarButtonItem (appearance)
 
+ (void) setupAppearance;
 
@end
UIBarButtonItem+appearance.m
Objective-C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
#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

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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.