-->

UIBarButtonItem Title Text is Always Global Tint C

2019-07-15 08:13发布

问题:

iOS 7.0 and up.

In AppDelegate.m, I'm setting my global tintColor: self.window.tintColor = myGlobalTintColor;.

In my table view controller where I want a red trash can button in the navigation bar while editing the table view cells, I have this:

- (void)setEditing:(BOOL)editing animated:(BOOL)animate {
    [super setEditing:editing animated:animate];
    if (editing) { // Start editing
        self.deleteBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:self action:@selector(deleteButtonPressed)];
        [self.deleteBarButtonItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [UIColor redColor],  NSForegroundColorAttributeName, nil] forState:UIControlStateNormal];
        self.navigationItem.rightBarButtonItems = @[self.deleteBarButtonItem];
    }
}

Despite the line of code that is setting NSForegroundColorAttributeName to [UIColor redColor], I never see the red color. When the button it set to .enabled = YES, I simply see it in my global tint color, like all the other buttons:

I've combed through my code and I'm sure there are no other locations where I'm setting anything else to myGlobalTintColor. It's worth noting that this UITableViewController is instantiated from a storyboard. Commenting out the line in AppDelegate.m just sends everything back to the default blue tintColor, but I still don't get the red color on the delete button.

回答1:

It turns out the problem was that I was using a third party library, SDCAlertView, and I was setting

[[SDCAlertView appearance] setTintColor:globalTintColor];

For some reason, this caused all of my UINavigationBar objects to take on it's tintColor.

I fixed this by simply replacing the above code:

[[SDCAlertView appearance] setButtonTextColor:globalTintColor];