最好的解决方案之一是创建一个UINavigationBar和UIToolbar类别。接下来的课程给你的iOS 4.0和iOS的解决方案5.0兼容性:
唯一的一点是,你需要调用next方法在你的viewDidLoad例如:所以
// Customizacion de navigation bar y toolbar compatible con iOS4 e iOS5
[UINavigationBar iOS5UINavigationBarBackgroundImage];
[UIToolbar iOS5UIToolbarBackgroundImage];
,为iOS类范畴4.0和iOS 5.0兼容性自定义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
迎接!
太棒了!谢谢。你(或其他人)是否知道以编程方式完成最后一步的方法?我没有使用Interface Builder设置我的导航控制器。 – Chris 2010-07-24 05:23:14
@Chris,我也想知道如何以编程方式覆盖工具栏。谢谢! – pixelfreak 2011-09-15 23:57:03
任何人都发现了如何以编程方式完成最后一步? – Panos 2012-07-09 13:03:43