1
votes

I have created in the XIB a custom UIButton with no image. When the button is clicked, the title disappears.

These answers didn't help me because, I do not have any image that can overlap the text

This didn't help either, because I do not have any background color

This one didn't help too

4
Can't reproduce your problem. Try creating the button with code and see if it still happens.joao
Share your code and screenshot.Baby Groot
In your xib, if you look at the different states of the button, do they have different titles?lnafziger
@HinataHyuga it is hard to do a screen shot because the title disappears only for a second.Luda
Added, glad that I could help! Also, all of the other answers had great ideas that could have been the problem and I upvoted each one for contributing good information and trying to help!lnafziger

4 Answers

4
votes

UIButton and its subclasses have 4 states which can change by code or stage config in IB

  1. normal
  2. highlighted
  3. selected
  4. disabled

Check UIButton.h in UIKit framework and you'll see how to use them:

- (void)setTitle:(NSString *)title forState:(UIControlState)state; 
- (void)setTitleColor:(UIColor *)color forState:(UIControlState)state;
- (void)setImage:(UIImage *)image forState:(UIControlState)state;  
- (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state;

If you setTitle, image, or backGroundImage for normal state, the default one of other states are same as normal one.

Default state is Normal, other state can be set:

[button setHighlighted:YES];
[button setSelected:YES];
[button setEnable:NO];//disable state

Button change from Normal to Highlighted on click, so if you want to keep normal title, please check:

-Don't use setImage or config image for normal and hightlighted state(just use backgroundImage)

-Don't setTitle:@"" forState:UIControlStateHighlighted or config to nothing in IB. -Don't setTitleColor or choose titleColor in IB same as UIButton backgroundColor.

You can test with 4 different titles and backGroundImages for 1 button and know how can it display.

1
votes

I have had this happen as well. What I believe the issue is, is since the button is default a white rounded button with the default "highlight" state of a button, and inverts the text color as the button is pressed, it does the same with a custom button. Now, since the text is a blue, and the background is white for a rounded button, it is quite the same in a custom button. The custom button's text will be whatever you want(set) it to be. The background is usually a transparent color as well, and since when the custom button is highlighted, it causes the text color to be inverted, just as a regular button does. This causes the textcolor to become transparent, and the background of the custom button just doesn't change because there is nothing to change to, hence the fact that the button is custom, (meaning you must set the highlight states and other attributes of the button). I have had this happen before, and I never really realized this before, but I hope this helps you out!

1
votes
// for setting text in normal state

[_myButton setTitle:@"myText" forState:UIControlStateNormal];


// show another text on touch

[_myButton setTitle:@"myText" forState:UIControlStateHighlighted];
1
votes

In an XIB, you can set the title for each of the different states by changing the State Config and then setting the title for the corresponding state. In the following example, I have set the Default state title to Title and the Highlighted state title to a single space, which makes it appear as blank in the app. Note however, that the preview only shows the Default settings, and does not update for the different configurations (see images below).

Default State Title
Default Title

Highlighted State Title
Highlighted Title