最好的解决方案之一是创建 UINavigationBar 和 UIToolbar 的类别。接下来的课程为您提供了 iOS 4.0 和 iOS 5.0 兼容性的解决方案:
唯一的事情是您需要在 viewDidLoad 中调用下一个方法,例如:
// Customizacion de navigation bar y toolbar compatible con iOS4 e iOS5
[UINavigationBar iOS5UINavigationBarBackgroundImage];
[UIToolbar iOS5UIToolbarBackgroundImage];
因此,适用于 iOS 4.0 和 iOS 5.0 兼容性的类 Category 用于自定义 BackgroundImage 结果如下:
@implementation UINavigationBar (BackgroundImage)
- (void)drawRect:(CGRect)rect
{
UIImage* img = [UIImage imageNamed: @"navigation-bg.png"];
[img drawInRect: CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
[self setTintColor:UIColorFromRGB(0x5ca666)];
}
+ (void) iOS5UINavigationBarBackgroundImage
{
if ([UINavigationBar respondsToSelector: @selector(appearance)])
{
[[UINavigationBar appearance] setBackgroundImage: [UIImage imageNamed: @"navigation-bg.png"] forBarMetrics: UIBarMetricsDefault];
[[UINavigationBar appearance] setTintColor:UIColorFromRGB(0x5ca666)];
}
}
@end
@implementation UIToolbar (BackgroundImage)
- (void)drawRect:(CGRect)rect
{
UIImage *image = [UIImage imageNamed: @"toolbar-bg.png"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
[self setTintColor:UIColorFromRGB(0x5ca666)];
}
+ (void) iOS5UIToolbarBackgroundImage
{
if ([UIToolbar respondsToSelector: @selector(appearance)])
{
[[UIToolbar appearance] setBackgroundImage:[UIImage imageNamed: @"toolbar-bg.png"] forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
[[UIToolbar appearance] setTintColor:UIColorFromRGB(0x5ca666)];
}
}
@end
问候!