。h文件
#import <UIKit/UIKit.h>
@interface UINavigationBar (setBackgroundColor)
- (void) setBackgroundColor:(UIImage *)backgroundImage;
@end
。m文件
#import "UINavigationBar+setBackgroundColor.h"
UIImage *newimage;
@implementation UINavigationBar (setBackgroundColor)
- (void) drawRect:(CGRect)rect {
UIImage *image = newimage;
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}
- (void) setBackgroundColor:(UIImage *)backgroundImage {
if ([UINavigationBar instancesRespondToSelector:
@selector(setBackgroundImage:forBarMetrics:)]) {
UIImage *image = backgroundImage;
[self setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];
}else{
newimage=backgroundImage;
[self setNeedsDisplay];
}
}
@end