UIBarButtonItem Title Text is Always Global Tint Color

醉酒当歌 提交于 2019-12-10 22:41:10

问题


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];


来源:https://stackoverflow.com/questions/23818808/uibarbuttonitem-title-text-is-always-global-tint-color

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!