按钮文本颜色的变化未反映

Posted

技术标签:

【中文标题】按钮文本颜色的变化未反映【英文标题】:Change in Button text color not reflecting 【发布时间】:2011-05-21 00:10:54 【问题描述】:

我遇到了一个奇怪的问题。当按钮突出显示时,我正在更改按钮的文本颜色,但视觉上没有任何变化。

[myButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[myButton setTitleColor:[UIColor colorWithRed:150.0 green:150.0 blue:150.0 alpha:1.0] forState:UIControlStateHighlighted];

我错过了什么吗?

【问题讨论】:

【参考方案1】:

试试:

[myButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[myButton setTitleColor:[UIColor colorWithRed:150.0/256.0 green:150.0/256.0 blue:150.0/256.0 alpha:1.0] forState:UIControlStateHighlighted];

颜色分量是介于 0.0 和 1.0 之间的浮点数!

【讨论】:

颜色分量在 0.0 和 1.0 之间浮动,但索引从 0 开始:float/255.0 应该是:[UIColor colorWithRed:150.0/255.0 green:150.0/255.0 blue:150.0/255.0 alpha:1.0]

以上是关于按钮文本颜色的变化未反映的主要内容,如果未能解决你的问题,请参考以下文章