我知道这个问题看起来和这里的其他问题一样,但是他们都没有说 ios4 和 ios5 之间不兼容。
在我的应用程序中,我想自定义导航栏的模式,但我的部署目标是 ios4,所以我在 appDelegate.m 的实现上方使用下面的代码来执行此操作:
@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 中模拟时,它不起作用,导航栏恢复为默认颜色..有什么帮助吗?
谢谢。
最佳答案
这是一个适用于 iOS 4 和 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
关于ios - ios 4 中的导航栏自定义在 ios5 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8255437/