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