1

我在 XIB 中创建了一个没有图像的自定义 UIButton。单击按钮时,标题消失。

这些答案对我没有帮助,因为我没有任何可以与文本重叠的图像

这也没有帮助,因为我没有任何背景颜色

这个也没有帮助

4

4 回答 4

4

UIButton 及其子类有 4 种状态,可以通过 IB 中的代码或阶段配置进行更改

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

检查 UIKit 框架中的 UIButton.h ,您将了解如何使用它们:

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

如果为正常状态设置标题、图像或背景图像,则默认其他状态之一与正常状态相同。

默认状态为正常,可设置其他状态:

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

单击时按钮从正常变为突出显示,因此如果要保持正常标题,请检查:

- 不要将 setImage 或配置图像用于正常和高亮状态(仅使用 backgroundImage)

- 不要 setTitle:@"" forState:UIControlStateHighlighted 或在 IB 中配置为空。- 不要在IB中设置TitleColor或选择与UIButton backgroundColor相同的titleColor。

您可以为 1 个按钮使用 4 个不同的标题和背景图像进行测试,并知道它如何显示。

于 2013-04-21T11:31:14.190 回答
1

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!

于 2013-05-05T21:47:29.397 回答
1
// for setting text in normal state

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


// show another text on touch

[_myButton setTitle:@"myText" forState:UIControlStateHighlighted];
于 2013-05-06T11:05:56.603 回答
1

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

于 2013-05-06T16:16:33.573 回答