我有一段代码创建了一个按钮,在正确的空间大小颜色等:
UIButton *learnmorebutton = [UIButton buttonWithType:UIButtonTypeCustom];
[self.view addSubview:learnmorebutton];
learnmorebutton.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.3];
learnmorebutton.frame = CGRectMake(0.0, 518.0, 159.0, 50.0);
[learnmorebutton setTitle:@"learn more" forState:UIControlStateNormal];
[learnmorebutton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
learnmorebutton.titleLabel.font = [UIFont fontWithName:@"Helvetica Light" size:17.0];
然后我在按下按钮时有一个触发器:
[learnmorebutton addTarget:self action:@selector(learnMoreClickEvent:) forControlEvents:UIControlEventTouchUpInside];
然后我想改变按钮背景和文本颜色,所以:
sender.titleLabel.textColor = [UIColor redColor];
sender.backgroundColor = [UIColor whiteColor];
背景颜色改变但文字消失。就像它也变白了一样。
最佳答案
你需要通过
设置标签的颜色[sender setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
按钮不接受标签上的颜色变化直接导致它的外观随着他的状态而变化。这就是为什么您需要使用特定的 UIButton API为此。
关于ios - 单击 IOS 后按钮标签颜色更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24682880/