Alexander Tkachenko
Alexander Tkachenko

Reputation: 3281

How to get rid of UIBarButtonItem's title label becomes dark on highlighted state

My problem is that I'm unable to set the exact color of UIBarButtonItem's title label on UIControlStateHighlighted. Seems that UINavigationBar adds some dark "color overlay" on the top of highlighted title label, and I don't know how to disable it.

I have UIBarButtonItem with title text that is configured via appearance, I need the exact colors for normal and highlighted state, so I've configured UIBarButtonItems this way:

UIBarButtonItem *barButtonItemAppearance = [UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil];

NSDictionary * textAttributes = @{
    NSForegroundColorAttributeName : blueColor,
};
[barButtonItemAppearance setTitleTextAttributes:textAttributes forState:UIControlStateNormal];

NSDictionary * selectedTextAttributes = @{
    NSForegroundColorAttributeName : whiteColor
};
[barButtonItemAppearance setTitleTextAttributes:selectedTextAttributes forState:UIControlStateHighlighted];

Code of adding UIBarButtonItem to UINavigationBar:

- (void)viewDidLoad {
    [super viewDidLoad];
    UIBarButtonItem *logoutButtonItem = [[UIBarButtonItem alloc] initWithTitle:title
                                                style:UIBarButtonItemStylePlain
                                               target:target
                                               action:action];
    [self.navigationItem setRightBarButtonItem:logoutButtonItem];         
}

Please see the attached screenshots.

enter image description here enter image description here

I want my Highlighted bar button to be just white. I tried to add UIButton as title to customView property of UIBarButtonItem, and configured UIButton for normal and highlighted states. THIS works great, i don't see just clear white color on highlighting:

UIButton *customButton = [UIButton buttonWithType:UIButtonTypeCustom];
[customButton setTitle:title forState:UIControlStateNormal];
[customButton setTitleColor:blueColor forState:UIControlStateNormal];
[customButton setTitleColor:whiteColor forState:UIControlStateHighlighted];
[customButton sizeToFit];
[customButton addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];

UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:customButton];

It fixed my problem, but maybe there is another way, more better solution, without adding UIButton?

Upvotes: 0

Views: 158

Answers (1)

Talha Ghous
Talha Ghous

Reputation: 63

Before add your uibarbutton simply set its normal and highlighted properties colors to your desired color.

Upvotes: 0

Related Questions