我有一段代码可以创建一个按钮,以正确的空格大小颜色等:
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的原因。