ios - ios 4 中的导航栏自定义在 ios5 中不起作用

标签 ios xcode uinavigationcontroller customization drawrect

我知道这个问题看起来和这里的其他问题一样,但是他们都没有说 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/

相关文章:

ios - iOS以NSString获取用户位置

ios - 有没有办法以编程方式创建应用程序 ID?

ios - 如何在 iOS 的谷歌登录应用程序中单击 "CANCEL "按钮后编写终止应用程序的代码

ios - 如何判断自定义 UISearchBar 中的 UITableView 是否被触摸?

iphone - 禁用操作 - 用户点击选项卡栏项目以转到 Root View Controller

ios - 使用现有字符串作为 valueForKey 值

ios - PrefixHeader.pch 文件找不到标题

ios - 无法获取当前类中弹出导航控件的概念

ios - UINavBar背景设置背景问题

ios - 旧版本的应用程序如何应对更新的核心数据模型?