我有一段代码可以创建一个按钮,使用正确的空间大小颜色等:
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];
背景会改变颜色,但文字会消失。就像它的白色一样。
答案 0 :(得分:1)
您需要通过
设置标签的颜色[sender setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
按钮不接受标签上的颜色变化,直接导致其外观随状态变化。这就是为什么你需要为此使用特定的UIButton API。
答案 1 :(得分:0)
[learnmorebutton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
更改为
[learnmorebutton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
当你点击uibutton时获得白色突出显示文本看起来像文本消失的原因。
答案 2 :(得分:0)
如果您询问“按下按钮时如何更改颜色”,您可以轻松更改它:
self.titleLabel.textColor = [UIColor redColor];
self.*yourlabel*.textColor = [UIColor *desiredColor*];