0

我有一个 UINavigationBar 类的子类。在 viewDidLoad 我有:

- (void)viewDidLoad
{
    [super viewDidLoad];

    UINavigationBar *bar = [[UINavigationBar alloc] init];

    NSString* path = [[NSBundle mainBundle] pathForResource:@"topbanner" ofType:@"png" inDirectory:@"assets"];

    NSData *data = [NSData dataWithContentsOfFile:path];

    UIImage *image = [UIImage imageWithData:data];

    [[UINavigationBar appearance] setBackgroundImage:image forBarMetrics:UIBarMetricsDefault]; //crash here!

    [self.view addSubview:bar];
}

我从我的 tableView 中调用它。当我在模拟器上运行时一切都很好,当我尝试在设备应用程序崩溃时运行。我有一个错误:

+[UINavigationBar appearance]: unrecognized selector sent to class 0x3e3fe490
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[UINavigationBar appearance]: unrecognized selector sent to class 0x3e3fe490'

为什么?

编辑:我在 Xcode 的蓝色目录文件夹中有我的 topbanner 文件。在模拟器中一切看起来都很好。

EDIT2:当然,当我在我的设备中删除这行代码时,我的应用看起来不错,但导航栏(oc)上没有图像。

4

2 回答 2

3

appearance方法是 iOS 5.0 的新方法。您可能正在使用带有 iOS 5.0 的模拟器,但您的设备可能使用较低的 iOS 版本。

最好在调用 respondsToSelector 之前使用此方法修改外观。

从 UINavigationBar 文档:

在 iOS v5.0 之前,当与导航控制器一起使用时,您只能对导航栏进行少量直接自定义。具体来说,可以修改 barStyle、tintColor 和 translucent 属性,但绝对不能直接更改 UIView 级别的属性,例如 frame、bounds、alpha 或 hidden 属性。此外,您应该让导航控制器管理导航项的堆栈,而不是尝试自己修改这些项。

http://developer.apple.com/library/ios/#documentation/uikit/reference/UINavigationBar_Class/Reference/UINavigationBar.html

于 2012-05-14T16:18:38.353 回答
1

你有没有尝试过:

[self.navigationController.navigationBar setBackgroundImage:YOURIMAGE forBarMetrics:UIBarMetricsDefault];
于 2012-05-14T16:21:44.847 回答