Rose1234
Rose1234

Reputation: 1

UIButton title hides on click of it

I have created a UIButton onject programmatically with a background image set to it. Its initial state is UIControlStateNormal.But when I click the button its title gets hidden and when I click and hold the button its title is visible in highlighted state. I have tried multiple combinations of various control states and events.But each time the title gets hidden. I am not getting what the problem is!! Please help.

Upvotes: 0

Views: 262

Answers (1)

isaac
isaac

Reputation: 4897

You should be able to achieve what I think you want (a constant title on the button, regardless of control state) by setting one title for both control states:

NSString *titleString = @"Hello World";
[aButton setTitle:titleString forState:UIControlStateNormal];
[aButton setTitle:titleString forState:UIControlStateHighlighted];

You may also wish to toggle the color of the title on highlight, if your title text color isn't contrasted sufficiently against your highlighted background:

[aButton setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
//OR
[aButton setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];

If it's your image giving you trouble, what you might want to simply disable image adjustment for highlighting:

[aButton setAdjustsImageWhenHighlighted:NO];

Upvotes: 2

Related Questions