ios4中的导航栏自定义在ios5中不起作用
我知道这个问题看起来和这里的其他问题一样,但是没有一个提到ios4和ios5之间的不兼容性 在我的应用程序中,我想自定义导航栏的模式,但我的部署目标是ios4,因此我使用appDelegate.m实现上面的代码来实现这一点:ios4中的导航栏自定义在ios5中不起作用,ios,xcode,uinavigationcontroller,customization,drawrect,Ios,Xcode,Uinavigationcontroller,Customization,Drawrect,我知道这个问题看起来和这里的其他问题一样,但是没有一个提到ios4和ios5之间的不兼容性 在我的应用程序中,我想自定义导航栏的模式,但我的部署目标是ios4,因此我使用appDelegate.m实现上面的代码来实现这一点: @implementation UINavigationBar (CustomImage) - (void)drawRect:(CGRect)rect { UIColor *color = [UIColor blackColor]; UIImage *image = [U
@implementation UINavigationBar (CustomImage)
- (void)drawRect:(CGRect)rect {
UIColor *color = [UIColor blackColor];
UIImage *image = [UIImage imageNamed: @"nav_bar.png"];
[image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
self.tintColor = color;
}
@end
当我使用4.3模拟器运行应用程序时,它工作正常,但当我在ios5中模拟时,它不工作,导航栏返回默认颜色。。有什么帮助吗
谢谢。对于iOS5,您可以使用以下方法:
if([navigationBar respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)] ) {
[navigationBar setBackgroundImage:[UIImage imageNamed: @"nav_bar.png"]; forBarMetrics: UIBarMetricsDefault];
}
我通常将其放入appDelegate中。我建议在iOS4上运行时使用现有代码,并使用新的iOS5功能自定义iOS5下的工具栏
请参见此处:这是一个适用于iOS 4和iOS 5的类别:
@implementation UINavigationBar (CustomBackground)
- (UIImage *)barBackground
{
return [UIImage imageNamed:@"top-navigation-bar.png"];
}
- (void)didMoveToSuperview
{
//iOS5 only
if ([self respondsToSelector:@selector(setBackgroundImage:forBarMetrics:)])
{
[self setBackgroundImage:[self barBackground] forBarMetrics:UIBarMetricsDefault];
}
}
//this doesn't work on iOS5 but is needed for iOS4 and earlier
- (void)drawRect:(CGRect)rect
{
//draw image
[[self barBackground] drawInRect:rect];
}
@end