Subclassed UIButton失去了亮点

时间:2013-07-16 14:37:02

标签: ios uibutton

所以我已经将UIButton子类化了,我添加到它的唯一方法是setTitle(我使用的是图标字体,因此我为通过图标表示的按钮创建了一个Icon_UIButton。)

我有这段代码:

Icon_UIButton* button = [[Icon_UIButton alloc] init]; 
[button setTitle:@"R"]; //R is a specific icon in my icon font
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];

这看起来很有效,但在我使用以下选项设置按钮后选择:

[button setSelected:YES];

每当我点击并按住该按钮时,它会再次变黑。

就像选中的Icon_UIButton一样,当它被同时选中时,它会忽略它的titleColor为突出显示的状态。

有没有人经历过这个?

1 个答案:

答案 0 :(得分:0)

好的,我在这个主题中找到了我的问题的正确答案:

Highlighted state for UIButton doesn't come when going from selected to normal state

我讨厌回答我自己的问题,但万一其他人在搜索字词中键入可能与谷歌中的这个问题匹配,这里是答案(缩写,但直接来自其他帖子):

还有另一个UIControlState(或者更确切地说,是混合的控制状态):

UIControlStateSelected | UIControlStateHighlighted

所以现在代码看起来像这样:

[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected | UIControlStateHighlighted];