gpt4 book ai didi

objective-c - 仅在设备上出现 UINavigationBar 外观错误

转载 作者:行者123 更新时间:2023-11-28 19:18:44 25 4
gpt4 key购买 nike

我有一个 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'

为什么?

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

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

最佳答案

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

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

来自 UINavigationBar 文档:

Prior to iOS v5.0, when used in conjunction with a navigation controller, there are only a handful of direct customizations you can make to the navigation bar. Specifically, it is alright to modify the barStyle, tintColor, and translucent properties, but you must never directly change UIView-level properties such as the frame, bounds, alpha, or hidden properties directly. In addition, you should let the navigation controller manage the stack of navigation items and not attempt to modify these items yourself.

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

关于objective-c - 仅在设备上出现 UINavigationBar 外观错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10587136/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com