Customizing tint color of UIBarButtonSystemItem wi

2019-06-21 05:57发布

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.

3条回答
啃猪蹄的小仙女
2楼-- · 2019-06-21 06:06

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).

查看更多
地球回转人心会变
3楼-- · 2019-06-21 06:18

I have solved my problem by changing the color of the bar button system item png file in an external editor, including that image to the project and loading it via

[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"UIButtonBarTrashBlack"] style:UIBarButtonItemStyleBordered target:self action:@selector(myAction)];

I have found the internal system image using the helpful UIKit-Artwork-Extractor.

I have edited the color of the artwork using the free software GIMP.

查看更多
萌系小妹纸
4楼-- · 2019-06-21 06:24

Instead of using black color, use this:

[UIColor colorWithRed:0 green:0 blue:0 alpha:1]; // [UIColor blackColor] makes it white

查看更多
登录 后发表回答