6
votes

I have an app in the app store that I'm using Flurry analytics on. And I keep getting an unhandled exception error from time to time that I can't figure out.

NSInvalidArgumentException: -[UIBarButtonItem setTintColor:]: unrecognized selector sent to instance 0x177b20 Msg: Application crashed

What I can't figure out is, I'm not setting any bar button items tint color anywhere. I have a few custom views where I am setting the right bar button item, but no tint.

Most of my uses of the button look like this.

- (void)viewDidLoad
{
    [super viewDidLoad];

    UINavigationBar *bar = [self.navigationController navigationBar];
    [bar setTintColor:[UIColor colorWithRed:0 green:69.0/255 blue:118.0/255 alpha:1]];
    self.navigationItem.title = @"Edit User";

    UIBarButtonItem *saveButton = [[UIBarButtonItem alloc] 
                                   initWithTitle:@"Save"
                                   style:UIBarButtonItemStylePlain 
                                   target:self
                                   action:@selector(editUser:)];
    self.navigationItem.rightBarButtonItem = saveButton;
    [saveButton release];

    UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] 
                                     initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
                                     target:self
                                     action:@selector(cancel)];

    [[self navigationItem] setLeftBarButtonItem:cancelButton];
    [cancelButton release];

}

If anyone has any insight into this issue, I would be very grateful. I am targeting iOS 4.0 and up in my project.

UPDATE: I figured out what was causing some of the random issues on the setTintColor. I found that I was setting the tint color on one of the actual bar button items. I'm guessing there are some differences between OS versions that can cause crashes. So if anyone can tell me an OS neutral way of setting a custom right bar button item in my navigation bar, it would be appreciated.

3
I've had some problems with the recently. Sometimes, you have to just call setTintColor on the NavigationController's subviews. ([[[self.navigationController.navigationBar subviews] objectAtIndex:1]setTintColor:[UIColor redColor]];) At least that fixed it for me.CodaFi

3 Answers

7
votes

The problem was with errant -setTintColor usage on 2 classes. -setTintColor is not supported on 4.x devices, so you will crash when older devices bump into the tint color.

3
votes

Have you tried :

self.navigationController.navigationBar.tintColor =[UIColor colorWithRed:0 green:69.0/255 blue:118.0/255 alpha:1];

?

1
votes

if your target is iOS 4.0 you can do this: In your AppDelegate.m in the end after @end put this code:

@implementation UINavigationBar (UINavigationBarCategory)
- (void)drawRect:(CGRect)rect {
    UIColor *color = [UIColor YOUR_COLOR];
    self.tintColor = color;
        //if you want image for background use this code
    UIImage *img  = [UIImage imageNamed: @"IMAGE_NAME.png"];
    [img drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}

@end

Hope this help. For me it's work.