的IOS5 *
我發現使用的UIBarButtonItem類的唯一方法:
的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
希望這是什麼你正在尋找。 信用這個解決方案去https://gist.github.com/2633081
不要ü要更改按鈕,背面的圖像點擊? – AppleDelegate
我已經定製了後退按鈕。我正在尋找不同的「普通」欄按鈕(比如iOS對「完成」和「保存」使用藍色按鈕,而對於「取消」則使用灰色)。 – James
@James,其中一個選項是使用自定義的Done按鈕作爲來自'UIBarButtonItem'的'CustomDoneBarButtonItem'並將其用作'[[CustomDoneBarButtonItem appearance] setBackgroundImage:image forState:UIControlStateNormal barMetrics:UIBarButtonItemStyleDone]'。無論何時添加完成按鈕,都要創建此自定義類的對象並添加它。 – iDev