ios - 突出显示时 UIButton 标题颜色发生变化 - 如何将其关闭?

标签 ios uibutton title textcolor

我创建了一个按钮。标题的颜色默认为黑色。但是当我按下它时,颜色变成了一点蓝色并且再也没有变回来,这是怎么发生的?谁能告诉我为什么?我希望按钮的标题始终保持黑色。我怎样才能做到这一点?我试过了

[button setTitleColor:[UIColor darkTextColor] forState:UIControlStateHighlighted];
[button setTitleColor:[UIColor darkTextColor] forState:UIControlStateSelected];

但是没有效果。当我在我的代码中添加这个时,按钮的标题似乎总是蓝色的。

代码如下。

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setFrame:CGRectMake(20, 360, 280, 44)];
[button setTitle:NSLocalizedString(@"Continue", @"Label: TextLabel in Continue button") forState:UIControlStateNormal];
button.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:20.0f];
button.titleLabel.textColor = [UIColor darkTextColor];
button.titleLabel.shadowColor = [UIColor blackColor];
button.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleRightMargin|UIViewAutoresizingFlexibleBottomMargin|UIViewAutoresizingFlexibleWidth;

[self.view addSubview:button];
[button release];

谢谢大家。我已经解决了这个问题。我认为根本原因是

button.titleLabel.textColor = [UIColor darkTextColor];

当我删除它并使用

button setTitleColor:(UIColor) forState:(UIControlState);

问题解决了!

最佳答案

你可以使用

[UIButton setTitleColor:forState:]

对于所有州,所有州的标题颜色将保持相同。

[button setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor redColor] forState:UIControlStateSelected];

注意:为避免重复输入或粘贴以上代码,您可以使用 Will 建议的以下代码,

[button setTitleColor:[UIColor redColor] forState:(UIControlStateHighlighted | UIControlStateNormal | UIControlStateSelected)];

关于ios - 突出显示时 UIButton 标题颜色发生变化 - 如何将其关闭?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13030933/

相关文章:

ios - NSFetchedResultsController 对空结果的自定义行为

ios - UIButton图像色调不起作用

css - 使用 html iframe 时显示标题标签 "popup"

ios - 设置按钮类型

swift - 在 Swift 3 的同一按钮上的图像上方的按钮上显示文本

jQuery:向元素添加标题属性并从以下元素插入文本

ios - 不可能更改 UIBarButtonItem 标题?

ios - 类型 'UIViewController' 的值没有成员 'mapView'

ios - 一个 UIView Swift 中的多个 TableView - 如何同时显示两者

ios - 通过代码在sqlite3中启用foreign_keys=ON