简体   繁体   中英

UINavigationBar apperance error on device ONLY

I have a subclass of UINavigationBar class. In viewDidLoad i have:

- (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];
}

And i call it from my tableView. When i run on simulator everything is fine, when i try to run on the device app crash. I have an error:

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

Why?

EDIT: my topbanner file i have in blue directory folder in Xcode. In emulator everything looks fine.

EDIT2: Of course, when i remove this line of code in my device my app looks good to, but there is no image on navigationbar (oc).

The appearance method is new to iOS 5.0. You are probably using the simulator with iOS 5.0, but your device might use a lower iOS version.

It is better to check with respondsToSelector before calling it to modify the appearance using this method.

From the UINavigationBar documentation:

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

你有没有尝试过:

[self.navigationController.navigationBar setBackgroundImage:YOURIMAGE forBarMetrics:UIBarMetricsDefault];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM