Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ios7 xcode 5 GM: color of UIBarButtonItem and selected UISegmentedControl part on iOS 6 device keep default color

Tags:

iphone

i'm porting now ios6 app to ios7 sdk (with Xcode 5 and mavericks) and i tried to change UIBarButtonItem color, here is what i try to do:

self.navigationController.navigationBar.tintColor 

- make changes color for bar but not for items

[[UINavigationBar appearance] setTintColor:[UIColor greenColor]];
[[UIBarButtonItem appearance] setTintColor:[UIColor greenColor]];

- doesn't work, same wrong color

self.navigationItem.rightBarButtonItem.tintColor = [UIColor greenColor];
self.navigationItem.leftBarButtonItem.tintColor = [UIColor greenColor];    

- doesn't work, same wrong color

    UIBarButtonItem *close = [[UIBarButtonItem alloc] 
                             initWithTitle:NSLocalizedString(@"Close",@"") 
                                     style:UIBarButtonItemStyleDone target:self
                                    action:@selector(closeAddressBook:)];

           close.tintColor = [UIColor greenColor];

- doesn't work, same wrong color

  for self.filterSegment.tintColor =  [UIColor greenColor] where UISegmentedControl *filterSegment;

i see unselected segment with correct color, but selected segment is a same wrong color.

any ideas?

like image 247
user170317 Avatar asked Sep 15 '13 21:09

user170317


3 Answers

Figured out what needs to be done, thanks to WWDC 2013 - Customizing Your App’s Appearance for iOS 7.

self.navigationController.navigationBar.tintColor = [UIColor redColor];

This will filter down into the other views in your app, so place on initial screen, and if you push to the next screen you will see that the back button is also red.

To change the navigation bar colour use

self.navigationController.navigationBar.barTintColor = [UIColor greenColor];

If you are making your app work for devices less than iOS7, you should check it responds to the selector

if([self.navigationController.navigationBar respondsToSelector:@selector(barTintColor)]) {

}
like image 194
DogCoffee Avatar answered Sep 23 '22 14:09

DogCoffee


For iOS7 this code works for me when I wish to change the colour of an individual UIBarButtonItem:

UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Title" style:UIBarButtonItemStyleBordered target:self action:nil];
[barButtonItem setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor redColor]} forState:UIControlStateNormal];
self.navigationItem.leftBarButtonItems = @[barButtonItem];
like image 26
Keith Kennedy Avatar answered Sep 25 '22 14:09

Keith Kennedy


It might be a good idea to set the tintColor property on your app's UIWindow instance instead. If you've got a standard 'accent' colour you're using throughout your app, this will tint every control in the app with that colour.

like image 45
rpowell Avatar answered Sep 23 '22 14:09

rpowell