I want a UIBarButtonItem with a custom image and blue text. I have made this by creating a UIButton with the text, font colour and image first, and then setting a custom view for the UIBarButtonItem.
The problem is that when I disable the UIBarButtonItem the text goes back to white. I have no idea why.
UIButton *customButton = [UIButton buttonWithType:UIButtonTypeCustom];
[customButton setBackgroundImage:[UIImage imageNamed:@"images/ButtonHeader"] forState:UIControlStateNormal];
[customButton setTitle:myDynamicText forState:UIControlStateNormal];
[customButton setFrame:CGRectMake(0.0f, 0.0f, 70.0f, 44.0f)];
[customButton addTarget:myTarget action:myAction forControlEvents:UIControlEventTouchUpInside];
[customButton.titleLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:12.0f]];
[customButton.titleLabel setTextColor:[UIColor blueColor]];
UIBarButtonItem *customBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:customButton];
self.navigationBar.rightBarButtonItem = customBarButtonItem;
self.navigationBar.rightBarButtonItem.enabled = NO;
What I really want to do is have different text colour for enabled and disabled state, but I can figure that part out myself if someone can tell me why the text turns white when enabled=NO, and how to stop that.
You shouldn’t be setting the font colour by getting the label like that. You need to configure it on the button
UIControlStateNormalwill act to set the default values, but then you can change the color for all the different states. A list of these states is available at the bottom of UIControl Class Reference underControl State