Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unrecognized selector sent on UIBarButtonItem setTintColor

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.

like image 298
Bill Burgess Avatar asked Dec 17 '11 14:12

Bill Burgess


3 Answers

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.

like image 185
Bill Burgess Avatar answered Oct 19 '22 23:10

Bill Burgess


Have you tried :

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

?

like image 43
Maulik Avatar answered Oct 19 '22 23:10

Maulik


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.

like image 31
donjordano Avatar answered Oct 20 '22 00:10

donjordano