Customizing tint color of UIBarButtonSystemItem with UIAppearance API

ilmiacs picture ilmiacs · Feb 21, 2013 · Viewed 7.3k times · Source

I know I can customize the UIBarButtonItem text via

setTitleTextAttributes:forState:

There is also a way to customize UITabBar icons via

setSelectedImageTintColor:

Is there a way to customize the tint color of a UIBarButtonSystemItem, (e.g. the trash icon color), just to have a consistent UX? I could not find anything.

If this is not possible, how would I proceed? Should I save a color modified version of the icon? Where can I find it? What would be the easiest way to modify it?

EDIT

To clarify, it's not the background color of the UIBarButtonItem I am asking for, but the color of the contour of the icon.

EDIT

Setting the tint color of the UIBarButtonItem yields the background color of the button set.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];


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

    UIBarButtonItem* trashButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:nil action:nil];

    trashButton.tintColor = [UIColor blackColor];
    UIViewController* viewController = [[UIViewController alloc] init];
    UINavigationController* navController = [[UINavigationController alloc] initWithRootViewController:viewController];
    [viewController.navigationItem setRightBarButtonItem:trashButton];

    self.window.rootViewController = navController;
    return YES;
}

yields this.

EDIT 2

It turns out that the contour color of the system icons in fact can be set via tintColor property of UIBarButtonItem, however only if its style property has the value UIBarButtonItemStylePlain. (Even then, some colors are special and leave the contour white. One such color is [UIColor blackColor]) However, using the UIBarButtonItem in a UINavigationBar the style is forced to be UIBarButtonItemStyleBordered. In this case, tintColor sets the background color of the button and leaves the contour white.

As I am using the UIBarButtonItems in a navigation bar, my problem still remains unsolved.

Answer

rmaddy picture rmaddy · Feb 21, 2013

Note: The following only works for buttons added to a toolbar, not a navbar.

Use the tintColor property of UIBarButtonItem. Either call it on a specific instance or call it on the appearance for all button items.

Be careful setting the tint for all button items via appearance. Not all button items look right when you set the tint.

Update:

UIBarButtonItem *btnAdd = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addAction)];
btnAdd.tintColor = [UIColor blackColor];

This will make the button's icon black (the background will be whatever the toolbar's tint is).